Updated 2014-08-31 15:04:53 by arjen

Arjen Markus (31 august 2014) There are countless of theorems and conjectures about the distribution of prime numbers. The Andrica conjecture states that the distance between two successive primes can not grow very fast. Expressed in mathematical form:
`sqrt(P(i+1)) - sqrt(P(i)) < 1`

where P(i) means the i-th prime.

Plotting this value as a function of i reveals some strange patterns, reminescent of Ulam's spiral. I got the idea of doing that from a posting by Antonio Sánchez Chinchón on a mathematics discussion group. All I did was implement his idea in Tcl.

If you run the program, you will clearly see an intriguing pattern.
```# andrica.tcl --
#     Generate a plot illustrating the Andrica conjecture
#
namespace eval ::primes {
variable primes {2 3 5 7 11 13 17}
variable nextPrimeCandidate 19
variable nextPrimeIncrement  1 ;# Examine numbers 6n+1 and 6n+5
}

# ComputeNextPrime --
#     Determine the next prime
#
# Arguments:
#     None
#
# Result:
#     None
#
# Side effects:
#     One prime added to the list of primes
#
# Note:
#     Using a true sieve of Erathostenes might be faster, but
#     this does work. Even computing the first ten thousand
#     does not seem to be slow.
#
proc ::primes::ComputeNextPrime {} {
variable primes
variable nextPrimeCandidate
variable nextPrimeIncrement

while {1} {
#
# Test the current candidate
#
set sqrtCandidate [expr {sqrt(\$nextPrimeCandidate)}]

set isprime 1
foreach p \$primes {
if { \$p > \$sqrtCandidate } {
break
}
if { \$nextPrimeCandidate % \$p == 0 } {
set isprime 0
break
}
}

if { \$isprime } {
lappend primes \$nextPrimeCandidate
}

#
# In any case get the next candidate
#
if { \$nextPrimeIncrement == 1 } {
set nextPrimeIncrement 5
set nextPrimeCandidate [expr {\$nextPrimeCandidate + 4}]
} else {
set nextPrimeIncrement 1
set nextPrimeCandidate [expr {\$nextPrimeCandidate + 2}]
}

if { \$isprime } {
break
}
}
}

# firstNprimes --
#     Return the first N primes
#
# Arguments:
#     number           Number of primes to return
#
# Result:
#     List of the first \$number primes
#
proc ::primes::firstNprimes {number} {
variable primes

while { [llength \$primes] < \$number } {
ComputeNextPrime
}

return [lrange \$primes 0 [expr {\$number-1}]]
}

#
# Create the plot
#
package require Plotchart

pack [canvas .c -width 800 -height 800]
set p [::Plotchart::createXYPlot .c {0 3000 500} {0 0.8 0.2}]

#
# Plot the first 3000 primes
#
set primes [::primes::firstNprimes 3000]

\$p dataconfig dot -type symbol

for {set i 0} {\$i < 2999} {incr i} {
set pi [lindex \$primes \$i]
set pn [lindex \$primes [expr {\$i+1}]]
set y [expr {sqrt(\$pn) - sqrt(\$pi)}]

\$p plot dot \$i \$y
}
```