Yes, I know about the Sieve of Eratosthenes and Euler, I'm trying to make one on my own, and see how quick it can be. Here's my code:
1 2 3 4 5 6 7 8 9 10
int isprime(double isprime)
{
double sqrtd= sqrt (isprime);
sqrtd*=10;
int sqrti= int(sqrtd);
if (sqrti%10!=0)
returntrue;
elsereturnfalse;
}
So basically, it square-roots the number, multiplies it by 10, and then determines if the last digit is a 0 or not.
The problem is, numbers like 26 have square roots of 5.099, and since 5.099 multiplied by 10 and converted to an int is 50, it is still counted as a prime because it ends in a 0.
I know I can just have a "sqrtd*=10;" and a "sqrtd*=100;", but who knows, maybe as numbers get higher, it will start to have more than just 1 zero after the decimal. Any ideas?
IS there any square roots of any number with more than one 0 after the decimal?
Oh dang, I must have been half asleep while typing...Hold on, editing my post. (AHH! What was I thinking? Nevermind guys, I'm clicking "Topic is Solved." Stupidity can take over sometimes. Sorry for wasting your time, my code doesn't work at all. I'm seeing what's wrong.)
I still don't think you should do it. It's confusing. That it works isn't an excuse to use it -- index[array] (instead of array[index]) shouldn't work but it does. That doesn't mean it's good.