Find adjacent primes which differ by a square integer

var p = 2
var upto = 1e6
 
each_prime(p.next_prime, upto, {|q|
    if (q-p > 36 && is_square(q-p)) {
        say "#{'%6s' % q} - #{'%6s' % p} = #{'%2s' % isqrt(q-p)}^2"
    }
    p = q
})

Output:

 89753 -  89689 =  8^2
107441 - 107377 =  8^2
288647 - 288583 =  8^2
368021 - 367957 =  8^2
381167 - 381103 =  8^2
396833 - 396733 = 10^2
400823 - 400759 =  8^2
445427 - 445363 =  8^2
623171 - 623107 =  8^2
625763 - 625699 =  8^2
637067 - 637003 =  8^2
710777 - 710713 =  8^2
725273 - 725209 =  8^2
779477 - 779413 =  8^2
801947 - 801883 =  8^2
803813 - 803749 =  8^2
821741 - 821677 =  8^2
832583 - 832519 =  8^2
838349 - 838249 = 10^2
844841 - 844777 =  8^2
883871 - 883807 =  8^2
912167 - 912103 =  8^2
919511 - 919447 =  8^2
954827 - 954763 =  8^2
981887 - 981823 =  8^2
997877 - 997813 =  8^2

Last updated