Find the sum of all the prime numbers larger than 2 less than 1 0 1 2 that are 1 more than a perfect square.
Because the number can get pretty big return the answer m o d 1 0 0 0 7
note the problem shouldn't take much more than one minute if your answer is taking too long consider looking for optimizations.
Based on: prime sum
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.
I have a question: How do you upload a coding environment into a solution/problem/note?
Log in to reply
just copy and paste, you can use this to format it
Testing 1 0 1 2 numbers for primality and then adding them up is just not feasible so we need to be smart about how we are testing. The first thing we need to do is realize that we only need to test 1 0 6 numbers by testing all values of n 2 + 1 for n in the interval [ 1 , 1 0 6 ] . The second thing is to realize that if you are testing if a number n divides m we only need to test the values of n on the interval [ 0 , m ] . This will make the problem at least feasible, but we can cut off a lot more time in it. Other than 2 there won't be any other even prime; for an integer n > 1 , n 2 + 1 can only be prime if n is even, so we only need to try the even values of n which shrinks our search by half. The same thing for the divisors of a number, if we have an odd number m we only need to try if odd numbers divide it, so the code looks like the code below. Another good solution that I did not implement but you are welcome to try is using the Pollard's rho algorithm.
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 |
|
( Plus @@ Table [ If [ PrimeQ [ i 2 + 1 ] , i 2 + 1 , Nothing ] , { i , 2 , 1 0 0 0 0 0 0 } ] m o d 1 0 0 0 7 ) ⟹ 3 6 0 1
Problem Loading...
Note Loading...
Set Loading...
Solution using Miller Rabin algorithm for primality testing: