Thanks Laurel . Have added another loop to check if the number is divisble by odd numbers and finally add to the number to the list oncethe number becomes greater than 2

#!/usr/bin/perl use warnings; use strict; ######## sub prime_factors { my $num = shift; my @primes = (); my $max = sqrt($num); while ($num % 2 == 0) { push @primes, 2; $num /= 2; } foreach (my $i = 3; $i <= $max; $i += 2) { if ($num % $i == 0) { push @primes, $i; $num /= $i; $max = sqrt($num); redo; } } if ($num > 2) { push @primes, $num; } return @primes; } foreach my $n ( (20, 48, 96, 7, 69, 33, 54, 13195, 600851475143, 2147 +483647, 29474100, 3217491003, 27000) ) { my @result = prime_factors($n); print "Number: $n , Prime_Factors: => @result", "\n"; } $time ./primes.pl Number: 20 , Prime_Factors: => 2 2 5 Number: 48 , Prime_Factors: => 2 2 2 2 3 Number: 96 , Prime_Factors: => 2 2 2 2 2 3 Number: 7 , Prime_Factors: => 7 Number: 69 , Prime_Factors: => 3 23 Number: 33 , Prime_Factors: => 3 11 Number: 54 , Prime_Factors: => 2 3 3 3 Number: 13195 , Prime_Factors: => 5 7 13 29 Number: 600851475143 , Prime_Factors: => 71 839 1471 6857 Number: 2147483647 , Prime_Factors: => 2147483647 Number: 29474100 , Prime_Factors: => 2 2 3 3 5 5 32749 Number: 3217491003 , Prime_Factors: => 3 32749 32749 Number: 27000 , Prime_Factors: => 2 2 2 3 3 3 5 5 5 real 0m0.015s user 0m0.009s sys 0m0.004s

In reply to Re^8: Avoid keeping larger lists in Memory by pr33
in thread Avoid keeping larger lists in Memory by pr33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.