Determine Prime Number: Simple Algorithm w/ While Loop

  • Thread starter teng125
  • Start date
  • Tags
    Prime
In summary, to determine whether a number is prime or not, a simple algorithm can be used such as going through every integer between 1 and the number and checking for divisors. It is enough to go up to the square root of the number and only test odd numbers that are not known multiples of other prime numbers. Using a remainder operator can help with testing for even division, and if no candidate factors divide evenly into the number, it is prime.
  • #1
teng125
416
0
can smby give me a simple example algorithm to determine whether the number is prime or not such as using while loop, if...then...else or others which is simple

thanx
 
Physics news on Phys.org
  • #3
but I'm looking for a simple example about prime number determination [algorithm] for programming language only using if...then... or while loop or repeat loops... that describe the steps of calculation
 
  • #4
teng125 said:
but I'm looking for a simple example about prime number determination [algorithm] for programming language only using if...then... or while loop or repeat loops... that describe the steps of calculation
How many of those google hits did you actually look through?
 
  • #5
A simple example I can think of is go through every integer between 1 and the prime number you want to test and check to see if it is a divisor.
 
  • #6
buddyholly9999 said:
A simple example I can think of is go through every integer between 1 and the prime number you want to test and check to see if it is a divisor.

you only need to go up to a third of the number. This is because if it is even then you know it is not prime, unless it is two. odd numbers are not divisble by 2, so the next divisor would be 3.
 
  • #7
a_ramage_1989 said:
you only need to go up to a third of the number. This is because if it is even then you know it is not prime, unless it is two. odd numbers are not divisble by 2, so the next divisor would be 3.

It's enough to go up to the square root of the number, much more efficient than having to check up to a third (still not at all fast though).
 
  • #8
and you only need to test (up to the [itex]\sqrt{N}[/itex] ) the odd numbers that are not known multiples of other prime numbers (like multiples of 3, 5, 7, 11, 13, etc). if your programming language has a remainder operator (like the "%" operator in C and C++), you can use that to test a candidate factor to see if it divides evenly into N. but if you don't, you have to divide, use the floor() or int() operator to get the integer part of the quotient, subtract the integer part from the quotient and, if that result is zero, it divides evenly.

if all candidate factors fail to divide evenly into N, then N is prime. if anyone candidate factor divides evenly into N, it is not prime.
 
  • #9
okok...thanx
 

1. What is a prime number?

A prime number is a positive integer that is only divisible by 1 and itself. In other words, it has exactly two factors.

2. Why is it important to determine prime numbers?

Prime numbers are important in many areas of mathematics and computer science. They are used in cryptography, prime factorization, and generating random numbers. They also have applications in fields such as number theory, algebra, and geometry.

3. How does the simple algorithm with while loop determine prime numbers?

The algorithm checks if a number is divisible by any number smaller than itself. If it is not, then it is a prime number. The while loop continues to check until it reaches the number itself or a smaller number that divides it evenly.

4. Are there any limitations to this simple algorithm?

Yes, the simple algorithm with while loop may take longer to determine prime numbers for very large numbers. In these cases, more efficient algorithms may be used.

5. Can this algorithm determine if a number is a prime number with 100% accuracy?

Yes, the simple algorithm with while loop can determine if a number is a prime number with 100% accuracy. However, it may take longer for larger numbers and there are more efficient algorithms available for larger numbers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
Replies
5
Views
396
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
22
Views
726
  • Engineering and Comp Sci Homework Help
Replies
1
Views
928
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • General Math
Replies
17
Views
532
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top