版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bainaqianhong/article/details/52808848
doit.cpp
#include <stdio.h>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <chrono>
unsigned int count_prime(int prime, int n) {
unsigned int ret = 0;
unsigned int temp = n/prime;
while(temp != 0) {
ret += temp;
temp /= prime;
}
return ret;
}
bool is_prime(int num) {
if (num <= 3) {
return num >= 2;
}
if (num%2==0 || num%3==0) {
return false;
}
for (int i = 5; i*i<=num; i+=6) {
if (num%i==0 || num%(i+2)==0) {
return false;
}
}
return true;
}
void show_message(std::vector<std::pair<int, unsigned int> > &data, bool showtoscreen, bool savetofile) {
std::stringstream ss;
ss << "{";
for (auto iter = data.begin(); iter != data.end(); ++iter) {
ss << iter->first << ": " << iter->second << ", ";
}
std::string message = ss.str();
if (!data.empty()) {
message.pop_back();
message.pop_back();
}
message.push_back('}');
if (savetofile) {
std::ofstream fout("./temp_message.txt", std::ios_base::out);
fout.write(message.c_str(), message.size());
fout.close();
}
if (showtoscreen) {
printf("%s\n", message.c_str());
}
return void();
}
void go(int n, std::vector<std::pair<int, unsigned int> > &ret) {
auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 2; i <= n; ++i) {
if (is_prime(i)) {
unsigned int count_num = count_prime(i, n);
ret.push_back(std::make_pair(i, count_num));
}
}
auto t1 = std::chrono::high_resolution_clock::now();
int64_t nanosecond = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count();
printf("time:%0.6fs\n", nanosecond*1.0/1e9);
}
bool is_prime(int num, std::vector<std::pair<int, unsigned int> >&refer) {
if (num <= 3) {
return num >= 2;
}
for (auto iter = refer.begin(); iter != refer.end(); ++iter) {
if (num%iter->first == 0) {
return false;
}
}
return true;
}
void go2(int n, std::vector<std::pair<int, unsigned int> > &ret) {
auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 2; i <= n; ++i) {
if (is_prime(i, ret)) {
unsigned int count_num = count_prime(i, n);
ret.push_back(std::make_pair(i, count_num));
}
}
auto t1 = std::chrono::high_resolution_clock::now();
int64_t nanosecond = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count();
printf("time:%0.6fs\n", nanosecond*1.0/1e9);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("please input the max num\n");
return 0;
}
int n = strtol(argv[1], NULL, 10);
std::vector<std::pair<int, unsigned int> > ret;
go(n, ret);
show_message(ret, n <= 500, n <= 1000000);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(doit)
set (lib_name doit)
add_executable(${lib_name} doit.cpp)
countprime.py
#coding=utf-8
import sys
import time
import getopt
def is_prime(value):
if value <= 3:
return value >= 2
if value%2 == 0 or value%3 == 0:
return False
i = 5
while i*i <= value:
if value%i == 0 or value%(i+2) == 0:
return False
i += 6
return True
def count_prime(n, prime):
ret = 0
temp = n/prime
while not temp == 0:
ret += temp
temp /= prime
return ret
def go(n):
li = []
for i in range(2, n+1):
if is_prime(i):
count = count_prime(n, i)
pair = (i, count)
li.append(pair)
return li
def main(argv):
if len(argv) >= 2:
n = int(argv[1])
t1 = time.clock()
li = go(n)
# time.sleep(1000)
t2 = time.clock()
if (n < 500):
print li
print"spend time:",t2-t1
else:
print "please input the argument"
if __name__ == '__main__':
main(sys.argv)