What is the greatest integer z less than 1 0 5 such that both z and the sum of digits of z are prime ?
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.
what is the algorithm behind the function is_prime ?
It's better to go down from 1 0 5 .
1 2 3 4 5 6 7 8 9 10 11 12 |
|
(Time:8.2 ms)
Here is a C++ code to find whether a number is prime or not.
1 2 3 4 5 6 7 8 |
|
I leave the further work on you all, if you don't mind :)
a) You don't need to test till
N/2
. Testing till
sqrt(N)
, or equivalently with the test condition
i*i<=N
suffices. Why? See
this
problem.
b) Your
is_prime()
uses
trial division
which is an inefficient method and will take a longer run-time as
N
increasing. To be precise, your
is_prime
has
O
(
N
)
time complexity. Try using faster primality tests like Miller-Rabin which has an average time complexity of
O
(
lo
g
3
N
)
.
In many cases we need can generate all the prime numbers in a range and then use some efficient search method to determine whether a given number in the range is equal to one of those primes. You might be interested in one of the pieces of Python software I happened upon just yesterday.
Actually it's written in C++ but it's accessible from Python. It's available at https://pypi.python.org/pypi/pyprimesieve. Very, very fast.
Problem Loading...
Note Loading...
Set Loading...