aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/max-kossek/perl/ch-1.pl
blob: 13403f10681fe1cf0c3df1ff877d3538edb6fc0e (plain)
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
39
#!/usr/bin/perl
use strict;
use warnings;

sub is_prime {
	my $num = shift;
	return $num > 1 if $num <= 3;
	return if $num % 2 == 0 || $num % 3 == 0;

	my $ctr = 5;
	while ($ctr * $ctr <= $num) {
		return if $num % $ctr == 0
			|| $num % ($ctr + 2) == 0;
		$ctr += 6;
	}

	return $num;
}

sub get_perfect_numbers {
	my $to_get = shift;
	$to_get = 5 if !defined $to_get;

	my @perfect_numbers = ();
	my ($got_so_far, $ctr) = 0;
	while ($got_so_far < $to_get) {
		$ctr++;
		if (is_prime(2**$ctr - 1)) {
			push @perfect_numbers, 2**($ctr-1) * (2**$ctr - 1);
			$got_so_far++;
		}
	}

	return @perfect_numbers;
}

print join ",", get_perfect_numbers(5);

__END__