Another translation of one of my Python snippets. This function will return a slice of consecutive prime numbers from 2 to a given value limit. In Go the 'int' type (int32) will go as high as 2147483647, but you can replace 'int' with 'uint64' and go as high as 18446744073709551615. |
+1 forum
0 | ||

I need to output in four columns. 1) The first will contain n. 2) The second will contain pi(n) 3) The third will contain n/ln(n) 4) The fourth will contain the ration of pi(n) to n/ln(n). #include <stdio.h> #include <string.h> #include <stdint.h> #define MAXN 100000000 /* maximum value of N */ #define P1 1562501 /* = ceil(MAXN/64) */ #define P2 50000000 /* = ceil(MAXN/2) */ #define P3 5000 /* = ceil(ceil(sqrt(MAXN))/2) */ uint32_t sieve[P1]; #define GET(b) ((sieve[(b)>>5]>>((b)&31))&1) void make() { uint32_t i, j, k; memset(sieve, 0, sizeof(sieve)); for (k = 1; k <= P3; k++) if (GET(k)==0) for(j=2*k+1,i=2*k*(k+1);i<P2;i+=j) sieve[i>>5]|=1<<(i&31); } … |
+0 forum
4 | ||

Here is simple math program to list number whose all factors are in given set of factors. I have included my timing decorator, which uses my timestring function, so I included that and imports for decorator in the decorator function, bit against the rules. Of course I could have used also the timeit module. I have implemented task by using previous results and expanding those and using modified sieve algorithm. For the sieve method you should provide your favorite prime_sieve in module utils or change the name according to your module/function name. |
+0 forum
0 |

The End.