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;

Dani AI

Generated

Good spot by — the original script failed to produce useful output because the printing and newline were not actually emitting the computed values. Beyond that immediate fix, make the script more robust so you don't get silent failures: enable use strict and use warnings, verify an argument was passed, and check it is a positive integer before factoring.

Here is a compact, safer approach that avoids repeatedly testing every candidate up to the full number and collects factors for a clean output:

use strict;
use warnings;

my $n = shift;
defined $n or die "Usage: $0 number\n";
die "Positive integer required\n" unless $n =~ /^\d+$/ && $n > 0;

my @factors;
while ($n % 2 == 0) { push @factors, 2; $n /= 2; }
for (my $p = 3; $p * $p <= $n; $p += 2) {
    while ($n % $p == 0) { push @factors, $p; $n /= $p; }
}
push @factors, $n if $n > 1;
print join(' ', @factors), "\n";

Notes and tips: factor out 2 first and then test only odd candidates up to sqrt(n) for a big speed win. Validate input so calling the script without an argument or with non-numeric data gives a clear error. For very large integers consider CPAN modules such as Math::Prime::Util or Math::BigInt; factoring large numbers can be slow or infeasible with a simple trial-division routine. If you still see no output, add simple debug warn lines to print values of the arguments and intermediate variables to confirm the script is actually receiving and processing the input.

Recommended Answers

All 2 Replies

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;

how i can convert from switch to if statment/ if statment to switch using #include<iostream.h>format of declaretion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.