Twin primes whose sum is square number

import "std/math.zc"
import "locale.h"

fn is_prime(n: int) -> bool {
    if n < 2      { return false; }
    if n % 2 == 0 { return n == 2; }
    if n % 3 == 0 { return n == 3; }
    let d = 5;
    while d * d <= n {
        if n % d == 0 { return false; }
        d += 2;
        if n % d == 0 { return false; }
        d += 4;
    }
    return true;
}

fn next_prime(n: int) -> int {
    if n < 2 { return 2; }
    n = !(n % 2) ? n + 1 : n + 2;
    loop {
        if is_prime(n) { return n; }
        n += 2;
    }
}

fn is_square(n: int) -> (bool, int) {
    let sqrt = (int)Math::sqrt((f64)n);
    if sqrt * sqrt == n { return (true, sqrt) };
    return (false, sqrt);
}

fn main() {
    setlocale(LC_NUMERIC, "");
    def LIM = 10_000_000;
    let p1 = 3;
    let count = 0;
    loop {
        let p2 = next_prime(p1);
        if p2 >= LIM { break; }
        if p2 == p1 + 2 {
            let sum = p1 + p2;
            let (is_sq, sqrt) = is_square(sum);
            if is_sq {
                printf("%'9d + %'9d = %'10d = %'5d²\n", p1, p2, sum, sqrt);
                count++;
            }
        }
        p1 = p2;
    }
    println "\nFound {count} twin primes < {LIM:'d} whose sum is a square number.";
}

Output:

17 +        19 =         36 =     6²
       71 +        73 =        144 =    12²
      881 +       883 =      1,764 =    42²
    1,151 +     1,153 =      2,304 =    48²
    2,591 +     2,593 =      5,184 =    72²
    3,527 +     3,529 =      7,056 =    84²
    4,049 +     4,051 =      8,100 =    90²
   15,137 +    15,139 =     30,276 =   174²
   20,807 +    20,809 =     41,616 =   204²
   34,847 +    34,849 =     69,696 =   264²
   46,817 +    46,819 =     93,636 =   306²
   69,191 +    69,193 =    138,384 =   372²
   83,231 +    83,233 =    166,464 =   408²
  103,967 +   103,969 =    207,936 =   456²
  112,337 +   112,339 =    224,676 =   474²
  149,057 +   149,059 =    298,116 =   546²
  176,417 +   176,419 =    352,836 =   594²
  179,999 +   180,001 =    360,000 =   600²
  206,081 +   206,083 =    412,164 =   642²
  281,249 +   281,251 =    562,500 =   750²
  362,951 +   362,953 =    725,904 =   852²
  388,961 +   388,963 =    777,924 =   882²
  438,047 +   438,049 =    876,096 =   936²
  472,391 +   472,393 =    944,784 =   972²
  478,241 +   478,243 =    956,484 =   978²
  538,721 +   538,723 =  1,077,444 = 1,038²
  649,799 +   649,801 =  1,299,600 = 1,140²
  734,471 +   734,473 =  1,468,944 = 1,212²
  808,991 +   808,993 =  1,617,984 = 1,272²
  960,497 +   960,499 =  1,920,996 = 1,386²
1,080,449 + 1,080,451 =  2,160,900 = 1,470²
1,143,071 + 1,143,073 =  2,286,144 = 1,512²
1,152,161 + 1,152,163 =  2,304,324 = 1,518²
1,254,527 + 1,254,529 =  2,509,056 = 1,584²
1,566,449 + 1,566,451 =  3,132,900 = 1,770²
1,685,447 + 1,685,449 =  3,370,896 = 1,836²
1,808,801 + 1,808,803 =  3,617,604 = 1,902²
1,960,199 + 1,960,201 =  3,920,400 = 1,980²
1,972,097 + 1,972,099 =  3,944,196 = 1,986²
2,268,449 + 2,268,451 =  4,536,900 = 2,130²
2,411,207 + 2,411,209 =  4,822,416 = 2,196²
2,544,767 + 2,544,769 =  5,089,536 = 2,256²
2,558,321 + 2,558,323 =  5,116,644 = 2,262²
2,681,927 + 2,681,929 =  5,363,856 = 2,316²
2,836,961 + 2,836,963 =  5,673,924 = 2,382²
3,516,551 + 3,516,553 =  7,033,104 = 2,652²
3,612,671 + 3,612,673 =  7,225,344 = 2,688²
3,693,761 + 3,693,763 =  7,387,524 = 2,718²
3,841,991 + 3,841,993 =  7,683,984 = 2,772²
3,959,297 + 3,959,299 =  7,918,596 = 2,814²
4,164,497 + 4,164,499 =  8,328,996 = 2,886²
4,339,457 + 4,339,459 =  8,678,916 = 2,946²
4,464,071 + 4,464,073 =  8,928,144 = 2,988²
4,536,071 + 4,536,073 =  9,072,144 = 3,012²
4,792,607 + 4,792,609 =  9,585,216 = 3,096²
5,385,761 + 5,385,763 = 10,771,524 = 3,282²
5,664,977 + 5,664,979 = 11,329,956 = 3,366²
6,118,001 + 6,118,003 = 12,236,004 = 3,498²
6,329,681 + 6,329,683 = 12,659,364 = 3,558²
6,415,361 + 6,415,363 = 12,830,724 = 3,582²
6,986,321 + 6,986,323 = 13,972,644 = 3,738²
7,166,897 + 7,166,899 = 14,333,796 = 3,786²
7,746,047 + 7,746,049 = 15,492,096 = 3,936²
7,793,351 + 7,793,353 = 15,586,704 = 3,948²
8,128,511 + 8,128,513 = 16,257,024 = 4,032²
8,694,449 + 8,694,451 = 17,388,900 = 4,170²
8,819,999 + 8,820,001 = 17,640,000 = 4,200²
9,305,297 + 9,305,299 = 18,610,596 = 4,314²
9,750,527 + 9,750,529 = 19,501,056 = 4,416²

Found 69 twin primes < 10,000,000 whose sum is a square number.

Attribution: This is a community solution for the Rosetta Code task Twin primes whose sum is square number in Zen C.

This article uses material from the Rosetta Code article Twin primes whose sum is square number, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.