## randomness in coin tosses and last digits of prime numbers

Posted in Books, Kids, R, Statistics, University life with tags , , , on October 7, 2014 by xi'an

A rather intriguing note that was arXived last week: it is essentially one page long and it compares the power law of the frequency range for the Bernoulli experiment with the power law of the frequency range for the distribution of the last digits of the first 10,000 prime numbers to conclude that the power is about the same. With a very long introduction about the nature of randomness that is unrelated with the experiment. And a call to a virtual coin toss website, instead of using R uniform generator… Actually the exact distribution is available, at least asymptotically, for the Bernoulli (coin tossing) case. Among other curiosities, a constant typo in the sign of the coefficient β for the power law. A limitation of the Bernoulli experiment to 10⁴ simulations, rather than the 10⁵ used for the prime numbers. And a conclusion that the distribution of the end digits is truly uniform which relates only to this single experiment!

## Le Monde puzzle [#840]

Posted in Books, Kids, R with tags , , , , on November 23, 2013 by xi'an

Another number theory Le Monde mathematical puzzles:

Find 2≤n≤50 such that the sequence {1,…,n} can be permuted into a sequence such that the sum of two consecutive terms is a prime number.

Now this is a problem with an R code solution:

library(pracma)
foundsol=TRUE
N=2
while (foundsol){

N=N+1
noseq=TRUE
uplim=10^6
t=0
while ((t<uplim)&&(noseq)){

randseq=sample(1:N)
sumseq=randseq[-1]+randseq[-N]
noseq=min(isprime(sumseq))==0
t=t+1
}

foundsol=!noseq
if (!noseq){
lastsol=randseq}else{ N=N-1}
}

which returns the solution as

> N
[1] 12
> lastsol
[1]  6  7 12 11  8  5  2  1  4  3 10  9

and so it seems there is no solution beyond N=12…

However, reading the solution in the next edition of Le Monde, the authors claim there are solutions up to 50. I wonder why the crude search above fails so suddenly, between 12 and 13! So instead I tried a recursive program that exploits the fact that subchains are also verifying  the same property:

findord=function(ens){

if (length(ens)==2){
sol=ens
foundsol=isprime(sum(ens))}
else{
but=sample(ens,1)
nut=findord(ens[ens!=but])
foundsol=FALSE
sol=ens
if (nut\$find){
tut=nut\$ord
foundsol=max(isprime(but+tut[1]),
isprime(but+tut[length(tut)]))
sol=c(tut,but)
if (isprime(but+tut[1]))
sol=c(but,tut)
}
}
list(find=foundsol,ord=sol)
}

And I ran the R code for N=13,14,…

> stop=TRUE
> while (stop){
+   a=findord(1:N)
+   stop=!(a\$find)}

until I reached N=20 for which the R code would not return a solution. Maybe the next step would be to store solutions in N before moving to N+1. This is just getting  me too far from a mere Saturday afternoon break.

## Le Monde puzzle [#820]

Posted in Books, Kids, R with tags , , , , , on May 15, 2013 by xi'an

The current puzzle is… puzzling:

Given the set {1,…,N} with N<61, one iterates the following procedure: take (x,y) within the set and replace the pair with the smallest divider of x+y (bar 1). What are the values of N such that the final value in the set is 61?

I find it puzzling because the way the pairs are selected impacts the final value. Or not, depending upon N. Using the following code (with factors() from the pracma package):

library(pracma)
endof=function(N){
coll=1:N
for (t in 1:(N-1)){

pair=sample(1:length(coll),2)
dive=min(factors(sum(coll[pair])))
coll=coll[-pair]
coll=c(coll,dive)
}
print(dive)
}

I got:

> for (t in 1:10) endof(10)
[1] 5
[1] 3
[1] 3
[1] 5
[1] 7
[1] 5
[1] 5
[1] 7
[1] 3
[1] 3> for (t in 1:10) endof(16)
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2
[1] 2

For N of the form 4k or 4k-1, the final number is always 2 while for N‘s of the form 4k-2 and 4k-3, the final number varies, sometimes producing 61’s. Although I could not find solutions for N less than 17…  Looking more closely into the sequence leading to 61, I could not see a pattern, apart from producing prime numbers as, in, e.g.

61 = 2 + [12 +  (4 + {14 + [13 + 16]})]

for N=17.  (Another puzzle is that 61 plays no particular role: a long run of random calls to endof() return all prime numbers up to 79…)

Udate: Looking at the solution in today’s edition, there exist a solution for N=13 and a solution for N=14. Even though my R code fails to spot it. Of course, an exhaustive search would be feasible in these two cases.  (I had also eliminated values below as not summing up to 61.) The argument for eliminating 4k and 4k-1 is that there must be an odd number of odd numbers in the collection, otherwise, the final number is always 2.

## odd prime numbers

Posted in Books with tags , , , , on February 9, 2013 by xi'an

Just read a puzzling fact in Le Monde science leaflet: someone at the “Great Internet Mersenne Prime Search” (GIMPS) just found a prime number with 17 million digits and…it is the 48th prime number of the form 2p-1. Which just means it is an odd number. How odd a remark!!! And how wrong. In fact, the short news item meant that this is a Mersenne number, of the form 2p-1! Another victim of a hasty cut&paste, I presume…

## Le Monde puzzle [#6]

Posted in R, Statistics with tags , , , , on February 18, 2011 by xi'an

A simple challenge in Le Monde this week: find the group of four primes such that any sum of three terms in the group is prime and the overall sum is minimised. Here is a quick exploration by simulation, using the schoolmath package (with its imperfections):

A=primes(start=1,end=53)[-1]
lengthA=length(A)

res=4*53
for (t in 1:10^4){

B=sample(A,4,prob=1/(1:lengthA))
sto=is.prim(sum(B[-1]))
for (j in 2:4)
sto=sto*is.prim(sum(B[-j]))

if ((sto)&(sum(B)<res)){
res=sum(B)
sol=B}
}
}

providing the solution 5 7 17 19.

A subsidiary question in the same puzzle is whether or not it is possible to find a group of five primes such that any sum of three terms is still prime. Running the above program with the proper substitutions of 4 by 5 does not produce any solution, even when increasing the upper boundary in A. So it is most likely that the answer is no.

## bug in schoolmath

Posted in R with tags , , , , on June 14, 2010 by xi'an

Neil Gunther has pointed out on his blog that the prime number decomposition R package schoolmath contains mistakes in the function primes, listing 1 as a prime number but also including decomposable numbers like 133 in its list of prime numbers:

> primes(100,140)
[1] 101 107 111 113 123 129 131 137
> primes(50,140)
[1]  51  53  59  61  67  71  73  79  83  89  97 101 103 107 109 113 127 131 133
[20] 137 139
> is.prim(primes(133)
[1] FALSE
> is.prim(primes(200,300))
[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE
[13] TRUE TRUE TRUE TRUE TRUE TRUE
> sum(1-is.prim(primes(1,1000)))
[1] 10
> data(primlist)
> sum(1-is.prim(primlist[1:25000]))
[1] 3309

This is rather annoying and I hope it gets quickly fixed!

## schoolmath

Posted in R with tags , , , on March 7, 2010 by xi'an

In connection with the Le Monde puzzle of last week, I was looking for an R function that would give me the prime factor decomposition of any integer. Such a function exists within the package schoolmath, developped by Joerg Schlarmann and Josef Wienand. It is called prime.factor and it returns the prime factors of any integer:

> prime.factor(2016)
[1] 2 2 2 2 2 3 3 7
> prime.factor(2032)
[1]   2   2   2   2 127
> prime.factor(2031)
[1]   3 677
> prime.factor(2039)
2039 is a prime!
[1] 2039

Warning [06/14/10]! As pointed out in this blog by Neil Gunther, schoolmath contains mistakes in the function primes, listing 1 as a prime number but also including decomposable numbers like 133.