in reply to Divisors for a number

Ok. Let's take it step by step:

First step: How would you find the divisors if you had no computer?

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Divisors for a number
by rasy (Initiate) on Mar 03, 2015 at 23:21 UTC
    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++;
      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