THINK FIRST·CODE LATER

← All labs

Prime factors

Problem

Every whole number from 2 up is a product of prime numbers: 360 = 2 × 2 × 2 × 3 × 3 × 5. PrimeFactors reads a number (at least 2) and prints this decomposition, the number of prime factors, and whether the number is prime.

Method: try the factor 2. While it divides the number, record it and divide; when it no longer divides, move to the next factor. Stop when the number has been reduced to 1. For the input

360

the program prints

360 = 2 x 2 x 2 x 3 x 3 x 5
Number of prime factors: 6
360 is not prime

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.

l7-9-data-1.txt

360
What your program must print for l7-9-data-1.txt
360 = 2 x 2 x 2 x 3 x 3 x 5
Number of prime factors: 6
360 is not prime

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 < l7-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l7-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.