How to Write a Prime Factorization Program in Computer

  • Context: High School 
  • Thread starter Thread starter DanMarino
  • Start date Start date
  • Tags Tags
    Computer Program
Click For Summary

Discussion Overview

The discussion focuses on how to write a computer program that performs prime factorization of a given number. Participants explore various methods and approaches, including algorithmic strategies and programming techniques.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant requests assistance in writing a program to break a number into its prime factors, using 24 as an example.
  • Another participant suggests using the Sieve of Eratosthenes to generate prime numbers as a potential method for the program, noting their previous experience but not providing specific code.
  • A different participant clarifies that while the Sieve of Eratosthenes generates primes, the actual factorization involves checking divisibility of the number by these primes, proposing a step-by-step method for doing so.
  • Further elaboration on the factorization process includes checking each prime against the number and recording factors until the quotient reaches 1, with an example using the number 24.
  • One participant emphasizes the need for a sufficiently large list of primes for the factorization process.
  • Another participant reiterates the importance of not requiring a list of primes larger than the square root of the number being factored, suggesting a more general approach.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and method of generating prime numbers for factorization, with some advocating for the Sieve of Eratosthenes while others propose alternative approaches. The discussion remains unresolved regarding the best method to implement prime factorization.

Contextual Notes

There are assumptions about the availability of prime numbers and the efficiency of various algorithms discussed, which may affect the implementation of the proposed methods.

DanMarino
Messages
2
Reaction score
0
i need to know how to write a program that will break a number down into its prime factors.
for example you would input the number 24 and the computer will break that up into its prime factors (2*2*2*3)
please help
thanks
 
Mathematics news on Phys.org
You can literally use Sieve of Eratosthenes if you want. Not too difficult to develop as a computer program. I've done it just a few years ago, but right now the code is not in a convenient place to refer. Maybe you can develop your own version or find through an internet search. I used arrays in mine and filled consecutive primes into the arrays. Maybe someone else knows a faster method. Maybe someone else knows something better than Sieve of Eratosthenes.
 
The sieve of Eratosthenes is a method of producing prime numbers but that is not the problem here. Here's what I would do:

1. First get a list of prime numbers at least up to the square root of n, perhaps using the sieve of Eratosthenes.

2. Step through your list of primes, testing whether the ith prime number, pi, divides n. If yes, record that fact and repeat with the quotient, n/pi until that quotient is 1. If you get up to the square root of n without finding any divisors, n is prime iteself.

With your example of 24, We would first check p1= 2. Does 2 divide 24? Yes, so record a 2. 24/2= 12. Does 2 divide 12? Yes, so record a 2. 12/2= 6. Does 2 divide 6? Yes, so record a 2. 6/2= 3. Does 2 divide 3? No so go on to p2= 3. Does 3 divide 3? Yes, so record a 3. 3/3= 1 so we are done.
 
HallsofIvy said:
Yes, so record a 3. 3/3= 1 so we are done.

This example has no large prime factors. If after finishing the loop a number more than 1 remains, it is prime and can simply be added to the list.
 
The problem was to factor a number. I assumed a sufficiently large list of primes was available.
 
HallsofIvy said:
The problem was to factor a number. I assumed a sufficiently large list of primes was available.

I was just explaining to give 'your' method generality without requiring a list of primes exceeding sqrt(n) in size.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
9K