rasy has asked for the wisdom of the Perl Monks concerning the following question:

Can some one help me to write a Perl script to get a number from the user and display the divisors of that number

Replies are listed 'Best First'.
Re: Divisors for a number
by hdb (Monsignor) on Mar 03, 2015 at 07:19 UTC

      How about some golf?

      ($%%$_)||print$_.$/for 1..($%=shift)
        > How about some golf?

        more obfuscation than golf. :)

        Beside the cryptic variable-name, is the first paren not needed and you can use say to shorten print.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

Re: Divisors for a number
by CountZero (Bishop) on Mar 03, 2015 at 07:38 UTC
    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
      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";
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Divisors for a number
by LanX (Saint) on Mar 03, 2015 at 02:19 UTC
    Sure!

    What did you try? ¹

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

    ¹) What, not even googling? ;)

      Like to learn to write this program in perl String r = ""; int p = 2; while (n > 1) if (n%p == 0) { r += (p+","); n /= p; } else p++;
Re: Divisors for a number
by locked_user sundialsvc4 (Abbot) on Mar 03, 2015 at 13:02 UTC
    Strange.   This fellow has now asked exactly the same homework question on a number of different forums . . .   It would have been much easier to just “figure it out.”
      Care to provide us with those crosslinks, so we don't waste time looking for them?

        I would assume that an application of such an algorithm is a stable of introductory CS or programming courses everywhere. It would not surprise me to find (multiple) such questions in the various venues that people tend ask them.