-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path387 Harshad Numbers.pl
More file actions
49 lines (35 loc) · 830 Bytes
/
387 Harshad Numbers.pl
File metadata and controls
49 lines (35 loc) · 830 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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 20 February 2017
# License: GPLv3
# https://github.com/trizen
# https://projecteuler.net/problem=387
# Runtime: 0.095s
use 5.010;
use strict;
use warnings;
use ntheory qw(:all);
my @valid;
my $limit = 10**14;
sub recurse {
my ($num, $sum) = @_;
if (($num . '01') >= $limit) {
return;
}
foreach my $n (0 .. 9) {
my $x = $num . $n;
my $y = $sum + $n;
if ($x % $y == 0) {
foreach my $i (1, 3, 7, 9) {
if ( ($x . $i) < $limit
and is_prime($x . $i)
and is_prime($x / $y)) {
push(@valid, $x . $i);
}
}
recurse($x, $y);
}
}
}
recurse($_, $_) for (1, 2, 4, 6, 8);
say vecsum(@valid);