/
ch-1.p6
62 lines (47 loc) · 1.73 KB
/
ch-1.p6
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
56
57
58
59
60
61
62
use v6;
################################################################################
=begin comment
Perl Weekly Challenge 012
=========================
Challenge #1
------------
The numbers formed by adding one to the products of the smallest primes are
called the Euclid Numbers (see [ https://en.wikipedia.org/wiki/Euclid_number
|wiki]). Write a script that finds the smallest *Euclid Number* that is not
prime. This challenge was proposed by Laurent Rosenfeld.
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2019 PerlMonk Athanasius #
#--------------------------------------#
use Math::Prime::Util:from<Perl5> < factor pn_primorial >;
my Sub $factor := &Math::Prime::Util::factor;
my Sub $pn-primorial := &Math::Prime::Util::pn_primorial;
my Str constant $FMT := "\nThe smallest composite Euclid number E_n is:\n" ~
" E_%d = (%d# + 1) = %s = %s\n";
sub MAIN()
{
for 1 ... Inf -> Int $n
{
unless (my Int $euclid = $pn-primorial($n) + 1).is-prime
{
$FMT.printf($n, $n, commify($euclid), $factor($euclid).join(' * '));
last;
}
}
}
sub commify(Int:D $n --> Str:D)
{
return "$n" if $n.abs < 1e3;
my @reversed = $n.split('', :skip-empty).reverse;
my @commified = @reversed.head;
for 1 .. @reversed.end - 1 -> Int $index
{
@commified.push: @reversed[$index];
my $next = $index + 1;
@commified.push: ',' if $next % 3 == 0 && @reversed[$next] ne '-';
}
@commified.push: @reversed.tail;
return @commified.reverse.join('');
}
################################################################################