115k views
5 votes
The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2 2 2 3 5

1 Answer

3 votes

Answer:

  • Class is created with name PrimeFactorisation.
  • isPrime(int num) is a method-> it will check whether the number given as a parameter is prime or not in time equal to O(sqrt(num)).
  • printPrimeFactors(int num) -> a recursive method for printing the prime factors.
  • Main method will be written to take a integer as input from user a.

We know that if a no x if divisible by y. than (y) and (x/y) are its factors and if they are prime numbers also , then they will be the prime factors.

Using this concept you can make recursive calls printPrimeFactors(y), printPrimeFactors(x/y).

Step-by-step explanation:

The code in java is explained below in image using comments before each statement.

The output image is also attached.

The prime factorization of a number is the unique list of prime numbers that, when-example-1
The prime factorization of a number is the unique list of prime numbers that, when-example-2
The prime factorization of a number is the unique list of prime numbers that, when-example-3
User Nayan
by
5.5k points