in reply to Re: Divisors for a number
in thread Divisors for a number

How can I write this program in perl String r = ""; int p = 2; while (n > 1) if (n%p == 0) { r += p+","; n /= p; } else p++;

Replies are listed 'Best First'.
Re^3: Divisors for a number
by choroba (Cardinal) on Mar 03, 2015 at 23:28 UTC
    The snippet you provided prints factorisation (i.e. only prime divisors, not all divisors). Translating it to Perl is straightforward:
    #!/usr/bin/perl use warnings; use strict; my $n = <>; # read the input my $r = ''; my $p = 2; while ($n > 1) { if ($n % $p == 0) { $r .= "$p, "; # . is the concatenation operator $n /= $p; } else { ++$p; } } print "$r\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you