You need to do some more checking. Right now, you are only checking if the number is divisible by 2. Do the same for 2, 3, 4, 5, 6, ... up to number nt: use a loop After you resolve this, try looking for optimizations.
You need to do some more checking. Right now, you are only checking if the number is divisible by 2. Do the same for 2, 3, 4, 5, 6, ... up to number.
After you resolve this, try looking for optimizations.
1 up to the number's root... – hillel Dec 12 '10 at 22:16 2 There are then many optimisations. You only need to test up to SQRT(n), since if n can be factored into 2 numbers one of them must be – James Gaunt Dec 12 '10 at 22:17 when it gets to optimization - think of the highest number to test. Does it have to be 'number' or can it be a bit less?
;) edit: arrrgh, people are fast today ;) – mad Dec 12 '10 at 22:18 1 Or even better than skipping even numbers, you can skip all numbers that aren't one less or one greater than a multiple of 6.(multiple of 6 + 2 or 4 is divisible by 2 and multiple of 6 + 3 is divisible by 3, leaving only multiples of 6 + 1 or 5) – user470379 Dec 12 '10 at 22:20 You can also take it a step further and check numbers based on their value modulo 30, which is still feasible to do as an unrolled loop. Though of course, you get diminishing returns as you go higher...(not checking multiples of 2 cuts the number of checks in half, based off 6 only takes another 1/3rd off that, going to 30 only eliminates another 1/5th...) – Anon. Dec 12 '10 at 22:24.
My own IsPrime() function, written and based on the deterministic variant of the famous Rabin-Miller algorithm, combined with optimized step brute forcing, giving you one of the fastest prime testing functions out there. __int64 power(int a, int n, int mod) { __int64 power=a,result=1; while(n) { if(n&1) result=(result*power)%mod; power=(power*power)%mod; n>>=1; } return result; } bool witness(int a, int n) { int t,u,i; __int64 prev,curr; u=n/2; t=1; while(!(u&1)) { u/=2; ++t; } prev=power(a,u,n); for(i=1;i=n-1)) return true; prev=curr; } if(curr! =1) return true; return false; } inline bool IsPrime( int number ) { if ( ( (!(number & 1)) && number!
= 2 ) || (number = 3) ) return (false); if(numberIf n */ } To use, copy and paste the code into the top of your program. Call it, and it returns a BOOL value, either true or false. If(IsPrime(number)) { cout It compiles fine under VS2008 and VS2010.
How it works: There are three parts to the function. Part checks to see if it is one of the rare exceptions (negative numbers, 1), and intercepts the running of the program. Part two starts if the number is smaller than 1373653, which is the theoretically number where the Rabin Miller algorithm will beat my optimized brute force function.
Then comes two levels of Rabin Miller, designed to minimize the number of witnesses needed. As most numbers that you'll be testing are under 4 billion, the probabilistic Rabin-Miller algorithm can be made deterministic by checking witnesses 2, 7, and 61. If you need to go over the 4 billion cap, you will need a large number library, and apply a modulus or bit shift modification to the power() function.
If you insist on a brute force method, here is just my optimized brute force IsPrime() function: inline bool IsPrime( int number ) { if ( ( (!(number & 1)) && number! = 2 ) || (number = 3) ) return (false); for( int k = 1; 36*k*k-12*k This piece is integrated into my larger IsPrime() function (the function shown first). If you need to find all the prime numbers below a number, find all the prime numbers below 1000, look into the Sieve of Eratosthenes.
Another favorite of mine.As an additional note, I would love to see anyone implement the Eliptical Curve Method algorithm, been wanting to see that implemented in C++ for a while now, I lost my implementation of it. Theoretically, it's even faster than the deterministic Rabin Miller algorithm I implemented, although I'm not sure if that's true for numbers under 4 billion.
If you are lazy, and have a lot of RAM, create a sieve of Eratosthenes which is practically a giant array from which you kicked all numbers that are not prime. From then on every prime "probability" test will be super quick. The upper limit for this solution for fast results is the amount of you RAM.
The upper limit for this solution for superslow results is your hard disk's capacity.
I would guess taking sqrt and running foreach frpm 2 to sqrt+1 if(input% number! =0) return false; once you reach sqrt+1 you can be sure its prime.
This code only checks if the number is divisible by two. For a number to be prime, it must not be evenly divisible by all integers less than itself. This can be naively implemented by checking if it is divisible by all integers less than floor(sqrt(n)) in a loop.
If you are interested, there are a number of much faster algorithms in existence.
If you know the range of the inputs (which you do since your function takes an int), you can precompute a table of primes less than or equal to the square root of the max input (2^31-1 in this case), and then test for divisibility by each prime in the table less than or equal to the square root of the number given.
That could done via the Sieve of Eratosthenes, but if the range is too large, there won't be enough memory for the system to use. And anyways, half of the memory would be wasted, all the even numbers are obviously composite. Guess a map (or a deque?
Forgot which is the C++ equivalent of a PHP associative array) would work best, to store all the prime numbers below a number, after you determine the prime numbers using the sieve. If it's not in the array, then it's composite. – LostInTheCode Dec 12 '10 at 23:08 @LostInTheCode There are only approximately 4300 or so numbers you would need in the table.
Reread what I wrote. I didn't say to create a table saying whether each number from 1 to 2^31 is prime -- I said to store only the prime numbers from 1 to sqrt(2^31) and test the number for divisibility by each of those numbers. – user470379 Dec 13 '10 at 19:36.
There are several different approches to this problem. The "Naive" Method: Try all (odd) numbers up to (the root of) the number. Improved "Naive" Method: Only try every 6n ± 1.
Probabilistic tests: Miller-Rabin, Solovay-Strasse, etc. Which approach suits you depends and what you are doing with the prime. You should atleast read up on Primality Testing.
One of my favorite pastimes, reading on Primality Testing. Always has been my favorite subject in Number Theory. Oh, and you left out Deterministic tests.
ECM and AKS seems to be leading that category. – LostInTheCode Dec 12 '10 at 22:59 Yes I didn't list all types of tests. I left that as an excercise for the reader.
;) – Sani Huttunen Dec 13 '10 at 15:50.
I follow same algorithm but different implementation that loop to sqrt(n) with step 2 only odd numbers because I check that if it is divisible by 2 or 2*k it is false. Here is my code public class PrimeTest { public static boolean isPrime(int i) { if (i Sqrt(i); j = j + 2) { if (i % j == 0) { return false; } } return true; } } /** * @param args */ public static void main(String args) { for (int I = 1; I.
Define TRUE 1 define FALSE -1 int main() { /* Local variables declaration */ int num = 0; int result = 0; /* Getting number from user for which max prime quadruplet value is to be found */ printf("\nEnter the number :"); scanf("%d", &num); result = Is_Prime( num ); /* Printing the result to standard output */ if (TRUE == result) printf("\n%d is a prime number\n", num); else printf("\n%d is not a prime number\n", num); return 0; } int Is_Prime( int num ) { int I = 0; /* Checking whether number is negative. If num is negative, making it positive */ if( 0 > num ) num = -num; /* Checking whether number is less than 2 */ if( 2 > num ) return FALSE; /* Checking if number is 2 */ if( 2 == num ) return TRUE; /* Checking whther number is even. Even numbers are not prime numbers */ if( 0 == ( num % 2 )) return FALSE; /* Checking whether the number is divisible by a smaller number 1 += 2, is done to skip checking divisibily by even numbers.
Iteration reduced to half */ for( I = 3; I.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.