that's my code :
#!/usr/bin/perl
use Math::BigInt;
print "Enter the big number : ";
$x = readline(*STDIN);
print "Enter the little number : ";
$y = readline(*STDIN);
$z = $x - $y;
$i = $z;
@all = '';
$j = 0;
system cls;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
while ($i ne 0) {
$t = $z / $i;
$k = Math::BigInt->new($t);
$k = $k->is_int();
if ($k eq "1") {$j++; $all[$j] = "$t\n";}
$i--; print "$i\n";
}
($sec1,$min1,$hour1,$mday,$mon,$year,$wday,$yday) = gmtime(time);
$data="Dis.txt";
open(DAT, $data) || die("Could not open file!");
@al=;
@al = @all;
open(DAT,">$data") || die("Cannot Open File");
print DAT @al;
close(DAT);
print "@all It took ", $min1 - $min, " minuts!\n";
$w = readline(*STDIN);
now how to make it find those numbers FASTER ?
20040705 Edit by [castaway]: Changed title from 'Didision'
For small numbers, this should be straightforward ...
@divisors = grep { $number % $_ == 0 } 1 .. $number;
I would not worry about optimizing this, unless you need it for very many or very big numbers ... but then it would be something like this:
# First, the lower half of the divisors:
@divisors = grep { $number % $_ == 0 } 1 .. sqrt($number);
# Then, the upper half:
push @divisors, map {$number == $_*$_ ? () : $number/$_} reverse @divisors;
(Sorry, I missed the Math::BigInt stuff ... but I guess the algorithm above can be adapted to that case as well?)
Update: Most of my code could benefit from comments. I added comments to the optimized version. Never code as if the guy who ends up maintaining your code can always maintain a proper caffeine intake.
print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!
and how will i know how many numbers are in @divisors in the second case ?
Well, this is basic Perl. To quote perldata:
If you evaluate an array in scalar context, it returns the length of the array.
So, scalar(@divisors) returns the number of divisors.
print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!
@divisors = grep { $number % $_ == 0 } 1 .. sqrt($number);
I think you are thinking of prime factorization here. You can stop there to determine primeness but not to determine all factors.
Cheers - L~R
so i need to know this $i...
$_.="\n" for @divisors;
# or
@divisors = map "$_\n", @divisors;
# or
for my $divisor (@divisors) {
$divisor = "$divisor\n";
}
# etc.
@divisors = grep { $number % $_ == 0 } 1 .. sqrt($number);
I think you are thinking of prime factorization here. You can stop there to determine primeness but not to determine all factors.
You somehow miss the second line of my optimized solution. It adds the other half of the divisors. :-)
push @divisors, map {$number == $_*$_ ? () : $number/$_} reverse @divisors;
For every divisor above the square root, there is a corresponding divisor below the square root. Symmetrical optimization. :-)
print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!
#!/usr/bin/perl
use strict;
use warnings;
my @factors = Factor_It( 20 );
print "$_\n" for @factors;
sub Factor_It {
my $number = shift;
my @factors;
for ( 2 .. int $number / 2 ) {
push @factors, $_ unless $number % $_;
}
return (1, @factors, $number);
}
Of course, you will also want to add some checks to ensure your input is a positive whole integer greater than 1.
Cheers - [Limbic~Region|L~R]
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $is = shift);
for (1 .. $is) {
print "$_ " if ($is % $_ == 0);
}
print "\n";
This sounds like a homework problem. Yet nonetheless, let me give a shot at answering your question. Basically what you are asking for is an algorithm that finds the smallest prime number a given number is divisible by (except for the multiplicative identity). So, in your case, the smallest prime that 20 is divisible by is 2, giving your a remainder of 10. The smallest prime 10 is divisible by is, again, 2, giving you a remainder of 5. Five, in itself is prime, so you're done.
So, 20 is made up of 2,2,5, and of course the multiplicative identity. Multiplying together the different combinations of the found primes, gives you the factors you were asked to find.
That's not at all the question that is being asked, and has nothing to do with primes. OP is asking for factorization all factors of a number.
Only numbers up to the square root of the large number need to be checked - not half.
use warnings; use strict; use integer; $|=1; print "Enter number: "; chomp(my $x =); 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";
perlmonks.org content © perlmonks.org and Anonymous Monk, chiburashka, gaal, gri6507, Limbic~Region, neniro, NodeReaper, pbeckingham, Sidhekin, ysth
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03