Created
November 24, 2018 06:16
-
-
Save d3x0r/7b99152089ee6549ba6d7c39a687de99 to your computer and use it in GitHub Desktop.
Quick remodel of primes test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <windows.h> // GetTickCount(); | |
struct primes { | |
int last; | |
int checkTo; | |
long long primes[100000]; | |
}; | |
struct primes * getPrimes( void ) { | |
struct primes *p = malloc( sizeof *p ); | |
p->last = 0; | |
p->checkTo = 0; | |
return p; | |
} | |
int total = 0; | |
int main() | |
{ | |
long long k; | |
int sets = 0; | |
int start = GetTickCount(); | |
struct primes *allprimes[1000]; | |
struct primes *primes = getPrimes(); | |
for (k = 2; k < 86028121; k++) | |
{ | |
int s; | |
if( primes->last == 100000 ) { | |
allprimes[sets++] = primes; | |
primes = getPrimes(); | |
} | |
for( s = 0; s < sets; s++ ) | |
if (!isPrime(allprimes[s], k)) { | |
break; | |
} | |
if( s == sets ) | |
if (isPrime(primes, k)) | |
{ | |
primes->primes[primes->last++] = k; | |
total++; | |
for( s = 0; s < sets; s++ ) { | |
int p; | |
for( p = allprimes[s]->checkTo; p < allprimes[s]->last; p++ ) { | |
if( allprimes[s]->primes[p] * allprimes[s]->primes[p] > k ) { | |
allprimes[s]->checkTo = p; | |
break; | |
} | |
} | |
} | |
if( s == sets ) | |
{ | |
int p; | |
for( p = primes->checkTo; p < primes->last; p++ ) { | |
if( primes->primes[p] * primes->primes[p] > k ) | |
break; | |
} | |
primes->checkTo = p; | |
} | |
//printf("X"); | |
} else { | |
//printf("X"); | |
//printf("O"); | |
} | |
if( ( (k%200000)==0 ) )printf( "%d (found)%d (checked) %lld of %d %g\n", GetTickCount() - start, total, k, 86028121, ((100.0*((double)k))/86028121.0) ); | |
} | |
} | |
int isPrime(struct primes *primes, long long number) | |
{ | |
int i; | |
long long *pll = primes->primes; | |
for (i = 0; i < primes->checkTo; pll++,i++ ) | |
{ | |
if (number % *pll == 0) | |
{ | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment