Determine Prime Number: Simple Algorithm w/ While Loop

  • Thread starter Thread starter teng125
  • Start date Start date
  • Tags Tags
    Prime
Click For Summary

Discussion Overview

The discussion centers around finding a simple algorithm to determine whether a number is prime, specifically using programming constructs such as while loops and if...then... statements. Participants seek clarity on the steps involved in the algorithm and share various approaches to the problem.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests a simple algorithm for prime number determination using basic programming constructs.
  • Another participant suggests using Google to find examples, indicating that there are many resources available online.
  • Several participants propose checking every integer up to the number being tested to see if it is a divisor, with some suggesting to limit checks to a third of the number.
  • Some participants argue that it is sufficient to check only up to the square root of the number for efficiency.
  • There is a suggestion to test only odd numbers that are not multiples of known primes, using a remainder operator to check for divisibility.
  • Participants discuss the method of using division and floor functions to determine if a number divides evenly.

Areas of Agreement / Disagreement

Participants express various methods for determining prime numbers, with no consensus on the most efficient approach. Some suggest checking up to a third of the number, while others advocate for checking only up to the square root. The discussion remains unresolved regarding the optimal algorithm.

Contextual Notes

Participants do not clarify the assumptions behind their proposed methods, such as the definitions of prime numbers or the efficiency of their algorithms. There are also unresolved mathematical steps in the proposed approaches.

teng125
Messages
416
Reaction score
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
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
 
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?
 
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.
 
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.
 
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).
 
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.
 
okok...thanx
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
5K
Replies
33
Views
5K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K