בדיקת ראשוניות..
2 posters
עמוד 1 מתוך 1
בדיקת ראשוניות..
למישהו יש את האלגורותם שבודק ראשוניות ב O של שורש N?
The_Imri- חפרן
- מספר הודעות : 53
Join date : 08.12.08
כן, בבקשה
אם יש בעיה אני אשלח את הקובץ, זה מתרגול 5 מ 4TEACH :
#include
using namespace std;
#include
void main()
{
int n;//input from user
int MaxTest;//largest number to be tested as divisor
int IsPrime = 1;//1 if n is prime, 0 if not
//get input
cout << "Primality test. Please enter an integer"
<< " you wish to test: ";
cin >> n;
//check primality
if(n <= 1)
IsPrime = 0;
else//n > 1
{
MaxTest = int(sqrt((double)n) + 0.5);//round to the int closest to sqrt(n)
for(int i = 2; (i <= MaxTest) && IsPrime; i++)
{
if(n%i == 0)//found a non-trivial divisor
{
IsPrime = 0;
}
}
}
//print output
if(IsPrime)
cout << n << " is prime\n";
else
cout << n << " is not prime.\n";
}
[left]
#include
using namespace std;
#include
void main()
{
int n;//input from user
int MaxTest;//largest number to be tested as divisor
int IsPrime = 1;//1 if n is prime, 0 if not
//get input
cout << "Primality test. Please enter an integer"
<< " you wish to test: ";
cin >> n;
//check primality
if(n <= 1)
IsPrime = 0;
else//n > 1
{
MaxTest = int(sqrt((double)n) + 0.5);//round to the int closest to sqrt(n)
for(int i = 2; (i <= MaxTest) && IsPrime; i++)
{
if(n%i == 0)//found a non-trivial divisor
{
IsPrime = 0;
}
}
}
//print output
if(IsPrime)
cout << n << " is prime\n";
else
cout << n << " is not prime.\n";
}
[left]
ItayAngel- חנבצ'
- מספר הודעות : 39
Join date : 10.12.08
Age : 37
Re: בדיקת ראשוניות..
ואו זה יצא דפוק
קיצר יש לי את הקובץ
קיצר יש לי את הקובץ
ItayAngel- חנבצ'
- מספר הודעות : 39
Join date : 10.12.08
Age : 37
עמוד 1 מתוך 1
Permissions in this forum:
אתה לא יכול להגיב לנושאים בפורום זה