THINK FIRST·CODE LATER

← All labs

The smallest multiplier that makes a perfect square

Problem

For a whole number m, find the smallest whole number n such that m × n is a perfect square. Example: 1500 = 2 × 2 × 3 × 5 × 5 × 5; the factors 3 and 5 appear an odd number of times, so n = 3 × 5 = 15 and 1500 × 15 = 22 500 = 150².

Write PerfectSquare with two methods:

  • primeFactors(long number) returns an ArrayList<Long> of all the prime factors, in increasing order;
  • factorsWithOddCount(ArrayList<Long> factors) returns the different factors that occur an odd number of times.

For the input

1500

the program prints

Prime factors of 1500: [2, 2, 3, 5, 5, 5]
Factors that appear an odd number of times: [3, 5]
Smallest n: 15
1500 x 15 = 22500 = 150 squared

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l13-9-data-1.txt

1500
What your program must print for l13-9-data-1.txt
Prime factors of 1500: [2, 2, 3, 5, 5, 5]
Factors that appear an odd number of times: [3, 5]
Smallest n: 15
1500 x 15 = 22500 = 150 squared

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l13-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l13-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.