The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
In C...
Codice:
/* Which prime, below one-million, can be written as the sum of the most consecutive primes? */
#include <stdio.h>
int primo(long); //funzione per verificare se un numero è primo o meno
int main()
{
long i, s=2, somma;
for(i = 3; i <= 1000000; i++)
{
if(primo(i)){
s += i;
if(primo(s) && (s <= 1000000))
somma = s;
}
}
printf("The prime that you need is --> %d\n", somma);
return 0;
}
int primo(long n)//se il numero non è primo mi torna 0, altrimenti 1
{
int i;
for(i = 2; i <= (int)n/2; i++)
{
if(n % i == 0)
return 0;
}
return 1;
}
Unico problema... ho fatto due calcoli e 953 (citato nel testo) non è la somma di numeri primi consecutivi... dovrebbe essere 963... Per cui o ho sbagliato la traduzione (molto probabile) oppure c'è un errorino nel testo...
Per il resto dovrebbe funzionare... Solo non capisco perchè quando lo avvio rimane fermo a lavorare e non mi restituisce nessun risultato (se voglio uscire devo arrestarlo manualmente)... Ovviamente gli ho lasciato tutto il tempo per calcolare... però nessun output...