Question

The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

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

Any solutions that do not use recursion will receive a grade of zero for all categories. (However, there are multiple good recursive solutions that exist for this problem.) Code for reading input and writing output has been provided in the Main class; your job is to complete the factorize() method. You may assume your input number is between 2 and 1,000, inclusive, and you may alter the starting code and/or use helper functions if you like.

//Starter code

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// read input number (which will be between 2 and 1000)
int number = Integer.parseInt(sc.nextLine());
  
// factor the number into an array of prime factors
int[] factors = factorize(number);
  
// print array of factors
for (int i = 0; i < factors.length; i++) {
System.out.print(factors[i]);
if (i != factors.length - 1) {
System.out.print(" ");
}
}
System.out.println();
}
  
public static int[] factorize(int number) {
// TODO implement this
//throw new UnsupportedOperationException();
  
  //Use a recursive solution, thanks
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class PrimeFactorization {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

// read input number (which will be between 2 and 1000)
        int number = Integer.parseInt(sc.nextLine());

// factor the number into an array of prime factors
        int[] factors = factorize(number);

// print array of factors
        for (int i = 0; i < factors.length; i++) {
            System.out.print(factors[i]);
            if (i != factors.length - 1) {
                System.out.print(" ");
            }
        }
        System.out.println();
    }

    public static int[] factorize(int number) {
        int n = number, count = 0;
        for (int i = 2; i <= number; i++) {
            while (n % i == 0) {
                ++count;
                n /= i;
            }
        }
        int[] arr = new int[count];
        int index = 0;
        n = number;
        for (int i = 2; i <= number; i++) {
            while (n % i == 0) {
                arr[index++] = i;
                n /= i;
            }
        }
        return arr;
    }
}

Add a comment
Know the answer?
Add Answer to:
The prime factorization of a number is the unique list of prime numbers that, when multiplied,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

    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...

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • // I need p and q for the bigInteger value of n.. prime factorization. example:n=7438638346346 find...

    // I need p and q for the bigInteger value of n.. prime factorization. example:n=7438638346346 find p and q public static void main (String[] args) throws java.lang.Exception { // your code goes here int n,p=1,q=0,k=0; n=11242573; while(k!=1){ p++; if((n%p)== 0){ q = n/p; k=1; } } System.out.print ( p +" "+ q); } }

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT