#HOW TO CHECK PRIME NUMBER WITHOUT LOOP?
57 messages ยท Page 1 of 1 (latest)
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.
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)
What? Ofc there is infinitely many prime numbers what are you talking about
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
should be || instead of &&, also 1 is not prime ๐
this proof was already known by euclid 2500 years ago!
is recursion allowed?
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
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
don't they use loops?
You can use MR test for like 9 bases in ifs :p but still there are loops under the hood for fast power
prolly yea but the primality test would only have if and else
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?
(there's an inf amount of primes it's been known for hundreds of years)
oh, look at me, contributing nothing helpful to the conversation...
the proof is quite elegant
correction: thousands
euclid showed it BC
you are right
it's almost as elegant as the proof that there's irrational numbers
i love math proofs
but we're beyond the point of the question :-/
i mean the question is kind of nonsensical / ambiguous so what would you expect
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
meh. seems simpler to just use a LUT of all primes < 2^64 and check if the number is in said LUT.
only 4e17 elements, shouldn't be too hard
piece of cake, that LUT would only occupy 3.326e18 bytes (about 2.88 petabytes)
how would you store it and make lookups?
exactly, that's a whole "big data" project on its own
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
nah i was kidding
ive given extremely bad advice LMAO
i guess my point is that it shouldnt be done this way
i figure that kinda got across
yeah for sure, i was just pedantically correcting your "obviously don't do it this way" code ๐
static array. binary search.
don't forget to add some blockchain
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
You can use sieve up to number and then check if the number is in the sieve
Oh oh i know! You can use my library!
Shill
Mhmm
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.