954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print the ptime factors of a number

Thanks for reading my post.
I just started perl programming.
I need to print the prime factors on the screen.
but when I executed my code, it shows nothing.
I dont know what else to do.
This is the code I got...

# number to factor is passed as an argument
$number = $ARGV[0];

# $left is the unfactored part that remains
$left = $number;

# loop through all possible factors
foreach $test (2..$number){

# exit when  no factoring left to do
if ($left == 1) {
  last;
    }

# doest $test divide $left?
if ($left % $test == 0) {

     $left /= $test;

 #print a space between factors
 if ($first) {
      print (" ");
    } else {
        $first = 1;
    }

    # now print the factor
     print ("test");

    # try this factor again
    redo ;
    }
}
print ('\n') if $first;
tenoran
Newbie Poster
9 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

Works Fine, What Are You Talking About ;):

#!/usr/bin/perl

# number to factor is passed as an argument
$number = $ARGV[0];

# $left is the unfactored part that remains
$left = $number;

# loop through all possible factors
foreach $test (2..$number){

        # exit when  no factoring left to do
        if ($left == 1) {
                last;
        }

        # doest $test divide $left?
        if ($left % $test == 0) {

                $left /= $test;

                #print a space between factors
                if ($first) {
                        print " ";
                } else {
                        $first = 1;
                }

                # now print the factor
                print "$test";

                # try this factor again
                redo ;
        }
}

print "\n" if $first;

Ps:
I changed this:

print ("test");

to this:

print "$test";


and this:

print ('\n') if $first;

to this:

print "\n" if $first;
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

how i can convert from switch to if statment/ if statment to switch using #includeformat of declaretion

barabala
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You