Print the Prime Factors of a Number

This function prints out the prime factorization of a number passed as an argument.

It uses a simple algorithm that loops through all possible factors (between 2 and the number itself), from smallest to largest, and divides the number by each factor that is found until it reaches 1. The factors should be printed out on one line, with a space separating each factor. Note: The inefficiency of the algorithm is not considered a bug.

Source Code

						1.     # number to factor is passed as an argument
 2.     $number = $ARGV[0];
 3.
						4.     # $left is the unfactored part that remains
 5.     $left = $number;
 6.
						7. # loop through all possible ...

Get Find the Bug A Book of Incorrect Programs now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.