-
Notifications
You must be signed in to change notification settings - Fork 34
/
binary_prime_encoder.pl
executable file
·79 lines (60 loc) · 1.51 KB
/
binary_prime_encoder.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
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 22 September 2016
# https://github.com/trizen
# Encode prime numbers below a certain limit into a large number.
# Example for primes below 7:
#
# x = 110101
#
# where each (k+1)-th bit in x is 1 when (k+1) is prime.
#
# This can be illustrated as:
# [1, 1, 0, 1, 0, 1]
# [2, 3, 4, 5, 6, 7]
#
# The binary number 110101 is represented by 53 in base 10.
# See also: https://oeis.org/A072762
# https://en.wikipedia.org/wiki/Prime_constant
use 5.010;
use strict;
use warnings;
no warnings 'recursion';
use Memoize qw(memoize);
use Math::AnyNum qw(:overload);
use ntheory qw(is_prime prev_prime);
memoize('_encode');
sub _encode {
my ($n) = @_;
$n < 2 ? 0 : 2 * _encode($n - 1) + (is_prime($n) ? 1 : 0);
}
sub encode_primes {
my ($limit) = @_;
_encode(prev_prime($limit + 1));
}
sub decode_primes {
my ($n) = @_;
my $pow = $n >> 1;
my $shift = 1;
while (($pow + 1) & $pow) {
$pow |= $pow >> $shift;
$shift <<= 1;
}
$pow += 1;
my @primes;
my $p = 2;
while ($pow) {
if ($n & $pow) {
push @primes, $p;
}
++$p;
$pow >>= 1;
}
@primes;
}
say "Encoded primes below 100: ", encode_primes(100);
say "Decoded primes below 100: ", join(' ', decode_primes(encode_primes(100)));
__END__
Encoded primes below 100: 65709066564613793476872782081
Decoded primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97