Determine if a Number is Prime

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)
		return true;
	else
		return false;
}


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?
Last edited on
So basically, it squares the number, multiplies it by 10, and then determines if the last digit is a 0 or not.

Why is this a condition for primality? With this logic 25 is a prime, as: sqrt(25)*10=5*10=50 = 0 (mod10)
Last edited on
26 isn't prime anyway; 26 / 2 = 13...
57 is a prime number.

-Grothendieck

P.S.- On a more serious note, you kinda need a new algorithm.
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.)
Last edited on
Raise your hand if you noticed that this shouldn't compile because "isprime" is used as both the function name and variable name.
this shouldn't compile because "isprime" is used as both the function name and variable name.

It compiles for me...
It shouldn't.
The local function variable name occludes the function name... so it compiles just fine. The trick is to properly reference the given objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int foo( int foo )
  {
  cout << foo << endl;
  if (foo > 1) return ::foo( foo - 1 );
  else         return        foo - 1;
  }

int main()
  {
  cout << foo( 3 ) << endl;
  return 0;
  }

Enjoy!
I didn't know that. ^_^

The more you knoooooooow...
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.
you could use floats and fmod instead of ints and the % operator
I have a function which as far as I know is reliably on getting primes:

Please excuse me if it's not optimized as I don't have a formal education of C/C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int f(int x)

{

int x;
static int divy,Mcounter;
    if (x <0) {x *= -1;}

	divy =1;

	Mcounter=0;

	for (int i =0;i <= x; i++)

	{

		divy++;

		1cout << "\n" << divy <<"\n" ;

	    rec = x % divy;

	    if( divy==x){divy += 1;continue;}

	    else if(rec == 0){Mcounter++;continue;}

	    else if (rec > 0){continue;}

	    

     }

     

     return Mcounter;
}

If it returns 0 it's prime. Otherwise it means that something divided evenly into it other than itself or one and thus not prime.
Last edited on
Topic archived. No new replies allowed.