Find the sum of all primes less than 1 million such that they are 1 more than a perfect square.
Note-As an explicit example, 2- which is 1 greater than 1 (a perfect square) is a prime and thus satisfies the above condition.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
C++ Code for the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The Output will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
1 2 |
|
Generate all perfect squares +1 < 1e6, filter for primality, and sum.
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range (5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
print sum(filter(is_prime, [(x ** 2) + 1 for x in range(1, 1000)]))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Python Code executed only in 1.0132 seconds
static bool checkPrime(long n) { if (n == 1) return false; if (n == 2 || n == 3) return true; if ((n + 1) % 6 != 0 && ((n - 1) % 6 != 0)) return false;
long i = Convert.ToInt64(Math.Sqrt(n));
for (; i >= 5; i--)
{
if ((i + 1) % 6 != 0 && ((i - 1) % 6 != 0)) continue;
if (n % i == 0)
return false;
}
return true;
}
How about a colourful solution :
PARI/GP ^^
while(prime(i)< 10^7,if( floor(sqrt(prime(i) - 1))-sqrt(prime(i)-1) == 0,s = s + prime(i);print(s));i = i+1);print(s)
Wolfram Mathematica Code:
1 2 3 |
|
It take 2.558416 sec to produce output: 29570385
Using Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
using namespace std;
bool p[1000001]={0}; int main(){ p[0]=1; p[1]=1; for(int i=2;i<1001;i++){ if(p[i]==0){ for(int j=i+i;j<=n;j+=i){ p[j]=1; } } } int sum=0; for(int i=1;i i+1<n;i++){ if(p[i i+1]==0){ sum+=i*i+1; } } cout<<sum; }
Try PHP version..
<html> <head> <title>Coding is fun - Vivek Sedani</title> </head> <body> <?php
function isprime($n){
if($n % 2 ==0 or $n == 1){ return false; }
$div = 2; $upto = floor( sqrt($n) );
while ($div <= $upto){
if($n % $div == 0){ return false; }
$div = $div + 1 + $div % 2;
}
return true;
}
$ans = 2; $div = 2;
while ( $div <= 1000){
$temp = $div * $div + 1;
if ( isprime ($temp) ){ $ans = $ans + $temp; }
$div = $div + 2;
}
echo $ans;
?> </body> </html>
Java code:
public class brilliant201410111904{
public static void main(String[] args){
long sum = 0;
for(int i=1;i<1000;i++){
if(isPrime(i*i+1)){
System.out.println(" "+(i*i+1));
sum += ((long)(i*i+1));
}
}
System.out.print(" "+sum+"\n ");
}
private static boolean isPrime(int n){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
}
Output:
2
5
17
37
101
197
257
401
577
677
1297
1601
2917
3137
4357
5477
7057
8101
8837
12101
13457
14401
15377
15877
16901
17957
21317
22501
24337
25601
28901
30977
32401
33857
41617
42437
44101
50177
52901
55697
57601
62501
65537
67601
69697
72901
78401
80657
90001
93637
98597
106277
115601
122501
147457
148997
156817
160001
164837
176401
184901
190097
193601
197137
215297
217157
220901
224677
240101
246017
287297
295937
309137
324901
331777
341057
352837
401957
404497
414737
417317
427717
454277
462401
470597
476101
484417
490001
495617
509797
512657
547601
562501
577601
583697
608401
614657
665857
682277
739601
746497
792101
820837
828101
846401
864901
876097
894917
902501
921601
933157
972197
29570385
Press any key to continue . . .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Problem Loading...
Note Loading...
Set Loading...
Runs in about a tenth of a second: