Create a prime writer

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
sigonasr2, Sig, Sigo 2022-07-14 15:08:36 +00:00 committed by GitHub
parent 43949f66df
commit 3919984b53
4 changed files with 2 additions and 55 deletions

View File

@ -32,6 +32,7 @@ int main(int argc,char**argv) {
}
if (primeVal%primes[i]==0) {
isPrime=0;
break;
}
}
if (isPrime) {

BIN
current

Binary file not shown.

1
primes Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,55 +0,0 @@
#include <stdio.h>
/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
https://projecteuler.net/problem=3
*/
int main(int argc,char**argv) {
long primes[10000];
int primeCount=1;
long primeVal=3;
long highestPrime=1;
primes[0]=2;
long startingNumb=600851475143;
label:
while (startingNumb>1) {
//find the next prime.
char isPrime=1;
for (int i=0;i<primeCount;i++) {
//first try all current primes.
if (startingNumb%primes[i]==0) {
//It's divisible!
startingNumb/=primes[i];
printf(" Factor: %ld\n",primes[i]);
if (highestPrime<primes[i]) {
highestPrime=primes[i];
}
goto label;
}
if (primeVal%primes[i]==0) {
isPrime=0;
}
}
if (isPrime) {
primes[primeCount++]=primeVal;
//printf("Generated a new prime: %ld\n",primeVal);
if (startingNumb%primeVal==0) {
//It's divisible!
startingNumb/=primeVal;
printf(" Factor: %ld\n",primeVal);
if (highestPrime<primeVal) {
highestPrime=primeVal;
}
}
}
primeVal++;
}
printf("Highest prime is %ld",highestPrime);
return 0;
}