File: abundant.pl

package info (click to toggle)
libmath-prime-util-perl 0.73-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,796 kB
  • sloc: perl: 24,676; ansic: 11,471; makefile: 26; python: 24
file content (38 lines) | stat: -rwxr-xr-x 1,102 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env perl
use strict;
use warnings;

# Find the first N abundant, deficient, or perfect numbers.

use Math::Prime::Util qw/divisor_sum next_prime is_prime/;

my $count = shift || 20;
my $type = lc(shift || 'abundant');

my $p = 0;
if ($type eq 'abundant') {
  while ($count-- > 0) {
    do { $p++ } while divisor_sum($p)-$p <= $p;
    print "$p\n";
  }
} elsif ($type eq 'deficient') {
  while ($count-- > 0) {
    do { $p++ } while divisor_sum($p)-$p >= $p;
    print "$p\n";
  }
} elsif ($type eq 'perfect') {
  # We'll use the chain of work by Euclid, Ibn al-Haytham, Euler, and others.
  # We just look for 2^(p-1)*(2^p-1) where 2^p-1 is prime.
  # Basically we're just finding Mersenne primes.
  # It's possible there are odd perfect numbers larger than 10^1500.
  do { require Math::BigInt;  Math::BigInt->import(try=>"GMP,Pari"); };
  while ($count-- > 0) {
    while (1) {
      $p = next_prime($p);
      last if is_prime(Math::BigInt->new(2)->bpow($p)->bdec);
    }
    print Math::BigInt->from_bin( '0b' . '1'x$p . '0'x($p-1) ), "\n";
  }
} else {
  die "Unknown type: $type\n";
}