#HOW TO CHECK PRIME NUMBER WITHOUT LOOP?

57 messages ยท Page 1 of 1 (latest)

crimson hollow
#

How to check prime number just by using if else statement?

marsh coveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

west plinth
#

if you have an infinikeyboard you could type

if (x == 1 && x == 2 && x == 3 && x == 5 && x = 7 && .......
.........
.............
...................
) {
   ... x is prime ...
}
#

you would just have to type all of the prime numbers

#

pretty sure they havent proved there is infinite prime numbers yet so you could probably use a regular keyboard but an infinikeyboard makes it easier

#

(truthfully, loop is probably the best way to check for prime :D)

wide jolt
#

If there was finitely many, let's say k prime numbers, then what about (P1*P2*P3...*Pk)+1?

#

On a side note, there are some relatively quick primarily tests for numbers < 2^64 (fitting into unsigned long long)

#

Most important being Miller Rabin test

#

In your case that's likely not what you are looking for

#

Create a function is_prime returning bool, that checks that the number has no divisors, then do if (is_prime(n)) ...

#

To get the "just an if" semantics

exotic basin
exotic basin
broken snow
#

is recursion allowed?

exotic basin
#

if your integer is 64 bits then in principle you have to check all possible prime divisors up to 2^32, and by the prime number theorem, there are approximately 2^32 / ln(2^32) = 1.9364e8 such primes, so the naive solution is gonna take a while

rough lodge
#

you could use Wilsons theorem and the gamma function in the stl to avoid using a loop for factorial but you're gonna run out of digits rather quickly

broken snow
#

don't they use loops?

wide jolt
#

You can use MR test for like 9 bases in ifs :p but still there are loops under the hood for fast power

rough lodge
broken snow
#

slightly off topic, but if you pre-compute E = 2 * 3 * 5 ... N - 1, where N = P * Q, then divide E by N, couldn't you binary search the missing prime numbers in the composite by precomputed sub-products of E / N?

rough sleet
#

oh, look at me, contributing nothing helpful to the conversation...

broken snow
#

the proof is quite elegant

broken snow
#

euclid showed it BC

rough sleet
#

you are right

rough sleet
#

i love math proofs

#

but we're beyond the point of the question :-/

broken snow
#

i mean the question is kind of nonsensical / ambiguous so what would you expect

rough sleet
#

true

#

im glad i'm no longer in school

#

actually, i lie. i liked the wonky stuff like this they gave. in real life, it's much less fun

#

in any case, i know very little cpp so im not a good influence lol

late eagle
exotic basin
broken snow
#

how would you store it and make lookups?

exotic basin
broken snow
#

yeah just slap a buzzword on it

#

hey, we could make an AI

#

using quantum computing and TPUs (buzzword in 3-5 years) to power high performance cloud computing on-prem

west plinth
#

i guess my point is that it shouldnt be done this way

#

i figure that kinda got across

exotic basin
late eagle
obsidian cloud
broken oriole
#

To answer, the most likely answer whatever this assignment is asking for is using recursion, you can write a naive one with using a helper function that takes the number and current divisor to check and return a bool

thorny jasper
#

You can use sieve up to number and then check if the number is in the sieve

wide jolt
#

Oh oh i know! You can use my library!

fierce kraken
wide jolt
#

Mhmm

marsh coveBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.