-
Notifications
You must be signed in to change notification settings - Fork 33
/
prime_41.pl
executable file
·70 lines (58 loc) · 1.44 KB
/
prime_41.pl
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
63
64
65
66
67
68
69
70
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 10 April 2015
# https://github.com/trizen
# The prime41() function.
# Inspired from: https://www.youtube.com/watch?v=3K-12i0jclM
# See more about this on: https://en.wikipedia.org/wiki/Formula_for_primes
use 5.010;
use strict;
use warnings;
use ntheory qw(is_prime divisors);
#
## A general form of: n^2 - n + 41
#
sub p41 {
my ($x, $y) = @_;
# $x: Nth number in the sequence
# $y: position in the sequence relative to 41
## Simple:
# $x**2 - $x + 41;
## General:
$x**2 + (2 * $x * $y) - $x + $y**2 - $y + 41;
}
foreach my $i (0 .. 100) {
my $n = p41($i, 1);
if (is_prime($n)) {
say "$i. $n - prime";
}
else {
say "$i. $n - not prime (factors: ", join(', ', grep { $_ != 1 and $_ != $n } divisors($n)), ")";
}
}
__END__
=> Deduced from:
43^2-2 = 1847 - prime
44^2-3 = 1933 - prime
45^2-4 = 2021 - not prime (factors: 43, 47)
46^2-5 = 2111 - prime
47^2-6 = 2203 - prime
48^2-7 = 2297 - prime
49^2-8 = 2393 - prime
50^2-9 = 2491 - not prime (factors: 47, 53)
51^2-10 = 2591 - prime
52^2-11 = 2693 - prime
53^2-12 = 2797 - prime
54^2-13 = 2903 - prime
55^2-14 = 3011 - prime
56^2-15 = 3121 - prime
57^2-16 = 3233 - not prime (factors: 53, 61)
58^2-17 = 3347 - prime
59^2-18 = 3463 - prime
60^2-19 = 3581 - prime
61^2-20 = 3701 - prime
62^2-21 = 3823 - prime
63^2-22 = 3947 - prime
64^2-23 = 4073 - prime
65^2-24 = 4201 - prime