-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path135 Same differences.pl
More file actions
55 lines (39 loc) · 929 Bytes
/
135 Same differences.pl
File metadata and controls
55 lines (39 loc) · 929 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
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 12 August 2017
# https://github.com/trizen
# https://projecteuler.net/problem=135
# Runtime: 3.973s
use 5.010;
use strict;
use warnings;
use ntheory qw(divisors);
sub has_ten_solutions {
my ($n) = @_;
my @divisors = divisors($n);
my $count = 0;
foreach my $divisor (@divisors) {
last if $divisor > sqrt($n);
my $p = $divisor;
my $q = $n / $divisor;
my $k = $q + $p;
($k % 4 == 0) ? ($k >>= 2) : next;
my $x1 = 3*$k - (($q - $p) >> 1);
my $x2 = 3*$k + (($q - $p) >> 1);
if (($x1 - 2*$k) > 0) {
++$count;
}
if ($x1 != $x2) {
return 0 if ++$count > 10;
}
}
return ($count == 10);
}
my $count = 0;
foreach my $n (1 .. 1e6 - 1) {
($n % 4 != 1)
&& has_ten_solutions($n)
&& $count++;
}
say $count;