Create a prime writer

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 2 years ago committed by GitHub
parent 43949f66df
commit 3919984b53
  1. 1
      archives/3/src/main.c
  2. BIN
      current
  3. 1
      primes
  4. 55
      src/main.c

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

Binary file not shown.

File diff suppressed because one or more lines are too long

@ -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;
}
Loading…
Cancel
Save