How does the prime number algorithm determine if a number is prime or not?

  • Context: Undergrad 
  • Thread starter Thread starter split
  • Start date Start date
  • Tags Tags
    Algorithm Prime
Click For Summary

Discussion Overview

The discussion revolves around the algorithm used to determine whether a number is prime, specifically focusing on the mathematical reasoning behind testing only up to the square root of the number in question. The scope includes theoretical aspects of prime number identification and mathematical reasoning.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes an algorithm that tests all numbers up to the square root of n to determine if n is prime.
  • Another participant questions the necessity of testing only up to the square root of n and seeks mathematical justification for this approach.
  • A different participant explains that if n is composite, it can be factored into two integers a and b, and at least one of these factors must be less than or equal to the square root of n, thus supporting the reasoning for the algorithm.
  • Additionally, one participant suggests that it is sufficient to test only the number 2 and all odd numbers up to the square root of n, rather than every integer.

Areas of Agreement / Disagreement

Participants present different aspects of the prime number algorithm, with some agreeing on the mathematical reasoning behind testing up to the square root of n, while others introduce variations in the testing process. The discussion does not reach a consensus on the optimal approach to testing for primality.

Contextual Notes

Some assumptions regarding the properties of composite numbers and prime factors are discussed, but the implications of these assumptions are not fully resolved. The discussion also does not clarify the completeness of the algorithm when only testing specific numbers.

split
Messages
25
Reaction score
0
When trying to determine whether a number is prime or not, the following algorithm is often used: Test all numbers up to [sqrt[n]] ([x] is the ceiling of x) to see if any divide evenly into n. If any do, the number is not prime.

My question is, why do you only have to test up to [sqrt[n]]? How does that work mathematically?
 
Mathematics news on Phys.org
If an integer n is composite, then you can factor it n=ab where a and b are both integers greater than 1. We can't have both a>sqrt(n) and b>sqrt(n), otherwise we'd get n>n. If a<=sqrt(n) then any prime dividing a (and we know there's at least one) is a prime factor of n that's less than sqrt(n). If b<=sqrt(n), we likewise get a prime factor of n less than sqrt(n). So if n is composite, it always has a prime factor <=sqrt(n), and it's enough to check these to verify whether n is composite or not.
 
Furthermore, you don't have to test every number up to the sqrt(n), just 2 and all of the odd ones.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 23 ·
Replies
23
Views
4K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K