ClassHelper News
About ClassHelper.org
Our Education Blog
Free Math Resources
Free School Clip Art
Crossword Puzzles
Word Find Puzzles
Cryptogram Puzzles
Word Jumble Puzzles
Coloring Book Pages
Computer Science
View Our Sitemap
Privacy Policy

Valid XHTML 1.0 Transitional


Perl Logo

Perl By Example: Factoring Numbers With Perl

Phil Paradis

By Philip C. Paradis
February 26, 2009

Today's Perl tutorial and example shows you how to write a subroutine that returns all the factors for a given input number. Our sub checks for a valid input value, determines all factors for the number, and returns the results in an array.

If you're very new to the Perl programming lanaguage, you may want to start with my beginner's introduction to Perl programming. If you have questions about installing Perl on your computer, you can reference the page on getting and installing Perl.

It's worth noting that this program is intended to be used in an educational context, and is not optimized for brevity or speed. While there are certainly more efficient methods of accomplishing this task, they would probably be more difficult to read and modify.

factor.pl
#!/usr/bin/perl -w

################################################################################
# factor.pl - Perl example for determining the factors of a number.            #
# Copyright (c) 2009 Philip C. Paradis (philip.paradis@classhelper.org).       #
# You may modify and distribute this code as you like, provided credit is      #
# given. A link to http://www.classhelper.org would be appreciated, but is not #
# required. Thank you!                                                         #
################################################################################

use strict;

# Define variables. -PCP
my	$input;
my	@factors;

# Set input and get factors. -PCP
$input = 302340;
@factors = &factor_number($input);

# If a zero length array is returned, the input was invalid. -PCP
if ($#factors == -1)
{
	print "The input \"$input\" is not a positive integer.\n";
	exit;
}

# Display the list of factors. -PCP
print "The factors of $input are:\n";
foreach (@factors)
{
	print "$_\n";
}

# If only two factors were returned, they must be the input number and 1.
# Display a message indicating the number is prime. -PCP
if ($#factors == 1)
{
	print "The number $input is prime.\n";
}

###############################################################################

sub factor_number
{
	# Declare variables and get input value. -PCP
	my	$input = $_[0];		# Input value. -PCP
	my	$counter;		# Counter for dividing operations. -PCP
	my	$div;			# Division result. -PCP
	my	@factors;		# Factors array. -PCP

	# A trick to force the input value to be treated as a number. -PCP
	$input += 0;

	# Make sure input is a positive integer. -PCP
	if (($input < 1) || ($input =~ /\./))
	{
		return ();
	}

	# A number is always divisible by itself. -PCP
	push (@factors, $input);

	# Start counter from two, up to half the value of the input. -PCP
	for ($counter = 2; $counter <= int($input / 2); $counter++)
	{
		$div = $input / $counter;
		unless ($div =~ /\./)
		{
			push (@factors, $div);
		}
	}

	# A number is always divisible by one. -PCP
	push (@factors, 1);

	# Return the array of factors. -PCP
	return (@factors);
}