How to Write a Prime Factorization Program in Computer

  • Thread starter Thread starter DanMarino
  • Start date Start date
  • Tags Tags
    Computer Program
Click For Summary
To write a program for prime factorization, start by generating a list of prime numbers up to the square root of the input number using the Sieve of Eratosthenes. Then, iterate through this list, checking if each prime divides the number, recording the prime and updating the quotient until it reaches 1. If a quotient greater than 1 remains after testing all primes up to the square root, that quotient is a prime factor itself. This method efficiently breaks down numbers like 24 into their prime factors, demonstrating a straightforward approach to factorization. Overall, the discussion emphasizes the importance of having a sufficient list of primes for effective factorization.
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.
 
Here is a little puzzle from the book 100 Geometric Games by Pierre Berloquin. The side of a small square is one meter long and the side of a larger square one and a half meters long. One vertex of the large square is at the center of the small square. The side of the large square cuts two sides of the small square into one- third parts and two-thirds parts. What is the area where the squares overlap?

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
966
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K