|
-
April 14th, 2009, 07:51 AM
#11
Re: Prime Numbers Help?
i write the code for you:
Code:
#include <cstdlib>
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int i, j;
for(i=2; i<100; i++)
{
bool isPrime = true;
for(j=2; j<=sqrt(i); j++)
{
if(i%j ==0 )
{
isPrime = false;
break;
}
}
if (isPrime)
{
cout<< i << " : Is Prime" << endl;
}else
{
cout<< i << " : Is not Prime" << endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
according to a math theory it is just enough to test for 0 remainder till reaching sqrt(i). so i used that theory to have faster algorithm
Last edited by toraj58; April 14th, 2009 at 07:54 AM.
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|