Hello I got a task, and I don't have any idea how to do it. Maybe you can help me?
There is given a whole number (that you need to insert by yourself using the command cin>>), and I need to determine is that number a prime or not. If it's a prime number I need to cout >> "1" and if it's not cout "0" , also it's mandatory to use the function:
bool IsPrime(long long a)
just think to yourself, what is the definition of a prime?
A prime is a number that has two divisors - 1 and itself, so if a number has 3 divisors it is not a prime number.
An algorithm to solve this is pretty simple,
test a number: let's say 83, check if all numbers between 2-82 can divide evenly into 83(hint use the % operator to test for an even number), if so return false, if we finish the loop and we have not returned false, we can return true
you don't actually have to test from 2- the Number, but rather 2 - square root of the number.
most of the prime stuff in the forum are about finding them.
there are a number of primality tests that can tell you if something is prime to a very, very high percentage probability with FAR less work. If you are willing to risk the small % of wrong answers. They are good enough for most tasks. You can do a web search on primality tests if you want to see what you can find and whether you want to do it that way.
for small primes (tens of millions?) you can just make a lookup table.