Find adjacent primes which differ by a square integer

From Rosetta Code
Revision as of 13:35, 21 November 2021 by Thebigh (talk | contribs) (add fermat)
Find adjacent primes which differ by a square integer 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.
Task


Find adjacents primes which difference (>36) is square integer under 1,000,000

C

<lang c>#include<stdio.h>

  1. include<stdlib.h>

int isprime( int p ) {

   int i;
   if(p==2) return 1;
   if(!(p%2)) return 0;
   for(i=3; i*i<=p; i+=2) {
      if(!(p%i)) return 0;
   }
   return 1;

}

int nextprime( int p ) {

   int i=0;
   if(p==0) return 2;
   if(p<3) return p+1;
   while(!isprime(++i + p));
   return i+p;

}

int issquare( int p ) {

   int i;
   for(i=0;i*i<p;i++);
   return i*i==p;

}

int main(void) {

   int i=3, j=2;
   for(i=3;j<=1000000;i=j) {
       j=nextprime(i);
       if(j-i>36&&issquare(j-i)) printf( "%d %d %d\n", i, j, j-i );
   }
   return 0;

}</lang>

Fermat

<lang fermat>Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.; i:=3; j:=3; while j<1000000 do

   j:=i+2;
   while j < 1000000 do
       if Isprime(j) then
           if j-i>36 and Issqr(j-i) then !!(i,j,j-i) fi;
           i:=j;
       fi;
       j:=j+2;
   od;

od;</lang>

FreeBASIC

<lang freebasic>#include "isprime.bas"

function nextprime( n as uinteger ) as uinteger

   'finds the next prime after n
   if n = 0 then return 2
   if n < 3 then return n + 1
   dim as integer q = n + 2
   while not isprime(q)
       q+=2
   wend
   return q

end function

function issquare( n as uinteger ) as boolean

   if int(sqr(n))^2 = n then return true else return false

end function

dim as uinteger i=3, j=0 while j<1000000

   j = nextprime(i)
   if j-i > 36 and issquare(j-i) then print i, j, j-i
   i = j

wend</lang>

Output:

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

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "./math" for Int import "./fmt" for Fmt

var limit = 1e6 - 1 var primes = Int.primeSieve(limit) System.print("Adjacent primes under 1,000,000 whose difference is a square > 36:") for (i in 1...primes.count) {

   var diff = primes[i] - primes[i-1]
   if (diff > 36) {
       var s = diff.sqrt.floor
       if (diff == s * s) {
           Fmt.print ("$,7d - $,7d = $3d = $2d x $2d", primes[i], primes[i-1], diff, s, s)
       }
   }

}</lang>

Output:
Adjacent primes under 1,000,000 whose difference is a square > 36:
 89,753 -  89,689 =  64 =  8 x  8
107,441 - 107,377 =  64 =  8 x  8
288,647 - 288,583 =  64 =  8 x  8
368,021 - 367,957 =  64 =  8 x  8
381,167 - 381,103 =  64 =  8 x  8
396,833 - 396,733 = 100 = 10 x 10
400,823 - 400,759 =  64 =  8 x  8
445,427 - 445,363 =  64 =  8 x  8
623,171 - 623,107 =  64 =  8 x  8
625,763 - 625,699 =  64 =  8 x  8
637,067 - 637,003 =  64 =  8 x  8
710,777 - 710,713 =  64 =  8 x  8
725,273 - 725,209 =  64 =  8 x  8
779,477 - 779,413 =  64 =  8 x  8
801,947 - 801,883 =  64 =  8 x  8
803,813 - 803,749 =  64 =  8 x  8
821,741 - 821,677 =  64 =  8 x  8
832,583 - 832,519 =  64 =  8 x  8
838,349 - 838,249 = 100 = 10 x 10
844,841 - 844,777 =  64 =  8 x  8
883,871 - 883,807 =  64 =  8 x  8
912,167 - 912,103 =  64 =  8 x  8
919,511 - 919,447 =  64 =  8 x  8
954,827 - 954,763 =  64 =  8 x  8
981,887 - 981,823 =  64 =  8 x  8
997,877 - 997,813 =  64 =  8 x  8

Python

<lang python> import math print("working...") limit = 1000000 Primes = [] oldPrime = 0 newPrime = 0 x = 0

def isPrime(n):

   for i in range(2,int(n**0.5)+1):
       if n%i==0:
           return False
   return True

def issquare(x): for n in range(x): if (x == n*n): return 1 return 0

for n in range(limit):

   if isPrime(n):
      Primes.append(n)

for n in range(2,len(Primes)):

   pr1 = Primes[n]
   pr2 = Primes[n-1]
   diff = pr1 - pr2
   flag = issquare(diff)
   if (flag == 1 and diff > 36):
      print(str(pr1) + " " + str(pr2) + " diff = " + str(diff))

print("done...") </lang>

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

Ring

<lang ring> load "stdlib.ring" see "working..." + nl limit = 1000000 Primes = [] oldPrime = 0 newPrime = 0 x = 0

for n = 1 to limit

   if isprime(n)
      add(Primes,n)
   ok

next

for n = 2 to len(Primes)

   pr1 = Primes[n]
   pr2 = Primes[n-1]
   diff = pr1 - pr2
   flag = issquare(diff)
   if flag = 1 and diff > 36
      see "" + pr1 + " " + pr2 + " diff = " + diff + nl
   ok

next

see "done..." + nl

func issquare(x)

    for n = 1 to sqrt(x)
        if x = pow(n,2)
           return 1
        ok
    next
    return 0

</lang>

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