-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler87.pl
More file actions
executable file
·56 lines (47 loc) · 875 Bytes
/
euler87.pl
File metadata and controls
executable file
·56 lines (47 loc) · 875 Bytes
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl -w
use strict;
my @prime = qw{ 2 3 5 };
my @sq = qw{ 4 9 25 };
my @cu = qw{ 8 27 125 };
my @fo = qw{ 16 81 625 };
my @cf;
my @all;
sub is_prime
{
my $t = shift;
foreach(@prime) {
return 0 unless ($t % $_);
last if $_**2 > $t;
}
1;
}
for ($_=7; $_<=7071; $_+=2) {
if (is_prime($_)) {
push @prime, $_;
push @sq, $_**2 if $_<=7071;
push @cu, $_**3 if $_<=368;
push @fo, $_**4 if $_<=84;
}
}
foreach (@fo) {
my $c = $_;
foreach (@cu) {
my $k = $c + $_;
push @cf, $k if ($k<50000000);
}
}
foreach (@sq) {
my $c = $_;
foreach (@cf) {
my $k = $c + $_;
push @all, $k if ($k<=50000000);
}
}
my $result = 0;
my $last = 0;
foreach (sort {$a<=>$b} @all) {
next if $_ == $last;
$last = $_;
$result++;
}
print $result."\n";