Prime Factors Program in Java and Python
Prime Factors program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to find the Prime factors of a number.
Prime factors of a number are those factors which are prime in nature and by which the number itself is completely divisible (1 will not be taken as prime number).
Few such numbers are: Prime Factors of 24 are 2, 2, 2, 3 Prime Factors of 6 are 2, 3
Algorithm:
Step 1: Start.
Step 2: Accept the number n.
Step 3: Initialize divisor d to 2.
Step 4: Repeat while n is greater than 1.
Step 5: If n % d is 0, display d as a prime factor.
Step 6: After displaying d, divide n by d and store the quotient back in n.
Step 7: Do not increment d immediately after successful division, because the same factor may repeat.
Step 8: If n % d is not 0, increment d to test the next possible divisor.
Step 9: Continue until n becomes 1.
Step 10: Stop.
Explanation:
Prime factorisation breaks a number into factors that are prime numbers. The program repeatedly divides the number by the smallest possible divisor.
The divisor starts from 2, the smallest prime number. If the current number is exactly divisible by the divisor, that divisor is printed as a prime factor.
After printing a factor, the number is divided by that factor. The same divisor is tested again because the same prime factor may occur more than once.
If the number is not divisible by the current divisor, the divisor is increased. This continues until the remaining number becomes 1, meaning all prime factors have been extracted.
When a divisor is successful, it is not increased immediately because the same prime factor may occur repeatedly. Only when division fails does the program move to the next possible divisor.
The divisor variable and the changing value of n work together. The loop repeatedly compares the remainder with zero, updates n after successful division, and increments the divisor only when the current divisor no longer divides n.
Prime factorisation breaks a number into factors that are prime. The program tests possible divisors starting from the smallest prime value. When a divisor divides the number exactly, it is printed or stored as a factor, and the number is divided by it. The same divisor is tested again because a prime factor may occur multiple times. Only when it no longer divides the number does the program move to the next possible divisor. This repeated reduction continues until the number is fully factorised.
Java Program:
/**
* The class PrimeFactors inputs a number and prints all its PrimeFactors
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class PrimeFactors
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter a Number : ");
n=sc.nextInt();
System.out.print("The Prime Factors of "+n+" are : ");
int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;
}
else
i++;
}
}
}Equivalent Python Program:
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.
n = int(input("Enter a number: "))
print("The Prime Factors of", n, "are:", end=" ")
i = 2
while n > 1:
if n % i == 0:
print(i, end=" ")
n = n // i
else:
i = i + 1Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.