Prime conspiracy: Difference between revisions

From Rosetta Code
Content deleted Content added
Thundergnat (talk | contribs)
m →‎{{header|Perl 6}}: Add Perl 6 example
Rdm (talk | contribs)
J draft
Line 33: Line 33:
9 → 9 count: 379 frequency: 3.79 %
9 → 9 count: 379 frequency: 3.79 %
</pre>
</pre>

=={{header|J}}==

This gets the job done:

<lang J> /:~ (~.,.~ ' ',.~ ":@,.@(#/.~)) 2 (,'->',])&":/\ 10|p:i.1e6
1 2->3
1 3->5
1 5->7
39603 7->7
39668 3->3
42843 9->9
42853 1->1
50153 1->9
58130 9->7
58255 3->1
64230 7->1
64371 9->3
68595 7->3
72827 3->7
77475 1->3
77586 7->9
79358 3->9
79453 1->7
84596 9->1</lang>

Note that the [[Sieve of Eratosthenes]] task has some important implications for how often we will see the various transitions here.

Anyways, here is how the code works:

'''p:i.1e6''' generates the first million primes.

'''10|''' ''...'' gets their last digits

'''2 ''...'' '''\''' pairs them up

'''(,'->',])&":/''' formats a pair of digits with a '->' between them

'''#/.~''' counts frequencies of unique values

'''~.''' gets the corresponding unique values

'''/:~''' sorts

And the rest is just more formatting...


=={{header|Perl 6}}==
=={{header|Perl 6}}==

Revision as of 22:31, 19 March 2016

Prime conspiracy is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Prime Conspiracy

A recent discovery, quoted from Quantamagazine , Marsh 23,2016 : "A previously unnoticed property of prime numbers seems to violate a longstanding assumption about how they behave. Prime numbers, it seems, have decided preferences about the final digits of the primes that immediately follow them."

The task is to check this assertion, modulo 10. Lets call i -> j a transition if i is the last decimal digit of a prime, and j the last decimal digit of the following prime.

Task

Considering the first 1_000_000 primes, count the number of transitions i -> j , print them along with their relative frequency. i and j are in (1,3,7,9) . Check that, for a given i, frequencies are not evenly distributed.

Extra credit : The same for 100_000_000 primes.

Example>>.words

10000 first primes. Transitions prime % 10 → next-prime % 10.
1 → 1 count:        365 frequency: 3.65 %
1 → 3 count:        833 frequency: 8.33 %
1 → 7 count:        889 frequency: 8.89 %
1 → 9 count:        397 frequency: 3.97 %
3 → 1 count:        529 frequency: 5.29 %
3 → 3 count:        324 frequency: 3.24 %
3 → 7 count:        754 frequency: 7.54 %
3 → 9 count:        907 frequency: 9.07 %
7 → 1 count:        655 frequency: 6.55 %
7 → 3 count:        722 frequency: 7.22 %
7 → 7 count:        323 frequency: 3.23 %
7 → 9 count:        808 frequency: 8.08 %
9 → 1 count:        935 frequency: 9.35 %
9 → 3 count:        636 frequency: 6.36 %
9 → 7 count:        541 frequency: 5.41 %
9 → 9 count:        379 frequency: 3.79 % 

J

This gets the job done:

<lang J> /:~ (~.,.~ ' ',.~ ":@,.@(#/.~)) 2 (,'->',])&":/\ 10|p:i.1e6

   1 2->3
   1 3->5
   1 5->7

39603 7->7 39668 3->3 42843 9->9 42853 1->1 50153 1->9 58130 9->7 58255 3->1 64230 7->1 64371 9->3 68595 7->3 72827 3->7 77475 1->3 77586 7->9 79358 3->9 79453 1->7 84596 9->1</lang>

Note that the Sieve of Eratosthenes task has some important implications for how often we will see the various transitions here.

Anyways, here is how the code works:

p:i.1e6 generates the first million primes.

10| ... gets their last digits

2 ... \ pairs them up

(,'->',])&":/ formats a pair of digits with a '->' between them

#/.~ counts frequencies of unique values

~. gets the corresponding unique values

/:~ sorts

And the rest is just more formatting...

Perl 6

Works with: Rakudo version 2016-02

<lang perl6>my @primes := 3, |(flat map( { $^i-1, $i+1 }, (6, 12, 18 ... *))).grep: *.is-prime;

my %conspiracy;

my $previous = 2;

my $upto = 1_000_000;

map { my $l = $_ % 10; %conspiracy{"$previous → $l count:"}++; $$previous = $l }, @primes[^$upto];

say "$_ \tfrequency: {($_.value/$upto *100).round(.01)} %" for %conspiracy.sort;</lang>

Output:
1 → 1 count:	42853 	frequency: 4.29 %
1 → 3 count:	77475 	frequency: 7.75 %
1 → 7 count:	79453 	frequency: 7.95 %
1 → 9 count:	50153 	frequency: 5.02 %
2 → 3 count:	1 	frequency: 0 %
3 → 1 count:	58255 	frequency: 5.83 %
3 → 3 count:	39668 	frequency: 3.97 %
3 → 5 count:	1 	frequency: 0 %
3 → 7 count:	72828 	frequency: 7.28 %
3 → 9 count:	79358 	frequency: 7.94 %
5 → 7 count:	1 	frequency: 0 %
7 → 1 count:	64230 	frequency: 6.42 %
7 → 3 count:	68595 	frequency: 6.86 %
7 → 7 count:	39603 	frequency: 3.96 %
7 → 9 count:	77586 	frequency: 7.76 %
9 → 1 count:	84596 	frequency: 8.46 %
9 → 3 count:	64371 	frequency: 6.44 %
9 → 7 count:	58130 	frequency: 5.81 %
9 → 9 count:	42843 	frequency: 4.28 %