1

I have this homework but I can't finish it, because I can only use relational operators and if-else/while, I can't use libraries and methods, only relational operators and If or while, I started checking If the number that is the limit is prime, First checking If / by 2 3 5 7 and 11 afther that I try % with every number before the square root of the number ( to determinate If it is prime) but it simply will need a lot of time to do this, how can I do to calculate it more with less time, sorry for the English an the rare explication.

  • Sounds more of a mathematical problem, look at this http://mathworld.wolfram.com/PrimalityTest.html and choose an algorithm, implement it in Java or Google an implementation in Java of one of those algorithms – Clive Makamara Oct 15 '17 at 05:03
  • 1
    Your question is hard to understand. But here's a fact for you. If you need your code to be **fast** you are either going to have to use arrays (and a sieve algorithm) or a probabilistic primality test which involves some complicated mathematics. Are your **SURE** that you teacher has asked you for a program that is fast? I suspect that he / she hasn't .... and you are just making life difficult for yourself! – Stephen C Oct 15 '17 at 05:11
  • 3
    From the teacher's perspective, the point of this exercise will be to get students to learn to write programs using arithmetic, relational operators and simple control structures. Fast algorithms for finding primes are almost certainly *out of scope* for an introductory programming course. My advice: just do what the requirements ask for, and spend the time that you saved on learning something else. – Stephen C Oct 15 '17 at 05:15
  • http://www.penguincoders.net/2015/05/find-nearest-prime-number-from-given-number.html – JHDev Oct 15 '17 at 07:00
  • If you are curious, here is a link to a probablilistic primality test: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test But this is typically only used for numbers much larger than the integer range because the basic methods are faster below that. – Henry Oct 15 '17 at 07:32

1 Answers1

0

Without arrays, the only other option to have some kind of sieve of Eratosthenes (for any hope to have better complexity than primality testing by trial division), that I know of, is with lazy lists, which can be emulated with generators. In pseudocode,

primes = [2..] \ [[p*p, p*p+p, ...] for p in primes]

with the starting point this becomes

primesFrom n = [n..] \ unionAll(
                    [[s,s+p..] for each p in psq
                               where psq = primes upto (2 * sqrt(n)),
                               where s   = div(n+p-1, p) * p] )

and we'd just take the first number produced by it. The union here is of course an ordered one, given the ordered, increasing sources, producing its results in an ordered fashion, one by one from smaller values to the bigger ones.

Twice the square root is used with the idea that the biggest prime gap in the range is covered. You can make it more precise by finding the precise value of the prime gap for your range. The psq primes can be found with trial division, also one by one, in a generator.

But now you need to maintain this collection of generators [s,s+p..] somehow, somewhere, and using arrays is still prohibited.

A linked list is one possibility. It too can be emulated, with nested recursive functions with local static storage in each closures, creating the chain of them on initialization, like

           = [n..] \ 
              ( [s1,s1+p1..] ∪
                ( [s2,s2+p2..] ∪
                  ( [s3,s3+p3..] ∪ (.... [sk,sk+pk..] ....))))

You can see an example of something just like this, in Go, here (and a faster one), although it implements the chain of \s (without the s),

   = (((....( [n..] \ 
                [s1,s1+p1..] ) \
                  [s2,s2+p2..] ) \
                    [s3,s3+p3..] ) \  ....) \ [sk,sk+pk..] 

which might be an easier thing to do altogether.

The s though can be arranged in a tree, for the additional complexity advantage. A fun project to dabble in, perhaps.

Will Ness
  • 62,652
  • 8
  • 86
  • 167