How to Write a Prime Factorization Program in Computer

  • Thread starter Thread starter DanMarino
  • Start date Start date
  • Tags Tags
    Computer Program
AI Thread 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.
 
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...

Similar threads

Back
Top