Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Finding all divisors of an integer

by ysth (Canon)
on Jul 04, 2004 at 08:29 UTC ( [id://371684]=note: print w/replies, xml ) Need Help??


in reply to Finding all divisors of an integer

Here's an implementation of gri6507's suggestion (using ordinary perl integers, not bigints):
use warnings; use strict; use integer; $|=1; print "Enter number: "; chomp(my $x = <STDIN>); die "That's not my kind of number!" if $x=~/\D/ || $x/1 ne $x; if ($x <= 1) { print "$x\n"; exit } # get prime factors my %fact; my $fact = 2; do { ++$fact while $x/$fact*$fact != $x; ++$fact{$fact}; $x /= $fact; } while ($x > 1); # apply all combinations my @fact = 1; while (($fact,my $count) = each %fact) { my $index; @fact = map $_ * $fact**(++$index/@fact % ($count+1)), (@fact) x ($count+1); } print "@{[sort {$a<=>$b} @fact]}\n";

Replies are listed 'Best First'.
Re^2: Finding all divisors of an integer
by Anonymous Monk on Jul 05, 2010 at 23:12 UTC
    oi? Newbie programer here.. dividing the number for primefactorization? sounds like this subroutine i cooked up to do my homework...
    sub primefactor ($) { my $number=shift; my @factors=(); for (my $i=2; $i<=$number;) { unless ($number%$i) { $number/=$i; push @factors, $i; } else {$i++} } @factors&&return(@factors); return($number); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://371684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-23 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found