user
Task

Write a program that displays twin primes under 10.000.000 whose sum is a square number.
assistant
Updated for updated task requirements. Trivial to find the squares that are sums of twin primes less than 1e7. (First 69.) Also find the largest square less than 1e12 (#4288) (Not sure why, but that seems to be a popular "stretch" task, and the largest matching *root* less than 1e7, that when squared is a sum of two twin primes. (#29,996) Added some timing info as well. Timing is going to be system dependent. This task is ridiculously parallelizable. Run on a 16 core Ryzen 7950X.

```
use List::Divvy;

my @tps = lazy (^Inf).hyper(:1000batch).map({next if $_%2; (my $s = .², $_, $s+>1)}).grep:
  {(.[2]-1).is-prime && (.[2]+1).is-prime};

{
    state $c;
    say "{++$c}: {.[1]}² ({.[0]}) == {.[2]-1} + {.[2]+1}"
} for @tps.&before({.[2] > 1e7});

say "{(now - INIT now).round(.001)} seconds elapsed\n";

{
    my $s =  @tps[$_];
    say "{1+$_}: {$s[1]}² ({$s[0]}) == {$s[2]-1} + {$s[2]+1}";
} for @tps.&before({.[0] > 1e12}).elems - 1, @tps.first({.[1] > 1e7},:k) - 1;

say "\n{(now - INIT now).round(.001)} total seconds elapsed\n";

```

Output:

```
1: 6² (36) == 17 + 19
2: 12² (144) == 71 + 73
3: 42² (1764) == 881 + 883
4: 48² (2304) == 1151 + 1153
5: 72² (5184) == 2591 + 2593
6: 84² (7056) == 3527 + 3529
7: 90² (8100) == 4049 + 4051
8: 174² (30276) == 15137 + 15139
9: 204² (41616) == 20807 + 20809
10: 264² (69696) == 34847 + 34849
11: 306² (93636) == 46817 + 46819
12: 372² (138384) == 69191 + 69193
13: 408² (166464) == 83231 + 83233
14: 456² (207936) == 103967 + 103969
15: 474² (224676) == 112337 + 112339
16: 546² (298116) == 149057 + 149059
17: 594² (352836) == 176417 + 176419
18: 600² (360000) == 179999 + 180001
19: 642² (412164) == 206081 + 206083
20: 750² (562500) == 281249 + 281251
21: 852² (725904) == 362951 + 362953
22: 882² (777924) == 388961 + 388963
23: 936² (876096) == 438047 + 438049
24: 972² (944784) == 472391 + 472393
25: 978² (956484) == 478241 + 478243
26: 1038² (1077444) == 538721 + 538723
27: 1140² (1299600) == 649799 + 649801
28: 1212² (1468944) == 734471 + 734473
29: 1272² (1617984) == 808991 + 808993
30: 1386² (1920996) == 960497 + 960499
31: 1470² (2160900) == 1080449 + 1080451
32: 1512² (2286144) == 1143071 + 1143073
33: 1518² (2304324) == 1152161 + 1152163
34: 1584² (2509056) == 1254527 + 1254529
35: 1770² (3132900) == 1566449 + 1566451
36: 1836² (3370896) == 1685447 + 1685449
37: 1902² (3617604) == 1808801 + 1808803
38: 1980² (3920400) == 1960199 + 1960201
39: 1986² (3944196) == 1972097 + 1972099
40: 2130² (4536900) == 2268449 + 2268451
41: 2196² (4822416) == 2411207 + 2411209
42: 2256² (5089536) == 2544767 + 2544769
43: 2262² (5116644) == 2558321 + 2558323
44: 2316² (5363856) == 2681927 + 2681929
45: 2382² (5673924) == 2836961 + 2836963
46: 2652² (7033104) == 3516551 + 3516553
47: 2688² (7225344) == 3612671 + 3612673
48: 2718² (7387524) == 3693761 + 3693763
49: 2772² (7683984) == 3841991 + 3841993
50: 2814² (7918596) == 3959297 + 3959299
51: 2886² (8328996) == 4164497 + 4164499
52: 2946² (8678916) == 4339457 + 4339459
53: 2988² (8928144) == 4464071 + 4464073
54: 3012² (9072144) == 4536071 + 4536073
55: 3096² (9585216) == 4792607 + 4792609
56: 3282² (10771524) == 5385761 + 5385763
57: 3366² (11329956) == 5664977 + 5664979
58: 3498² (12236004) == 6118001 + 6118003
59: 3558² (12659364) == 6329681 + 6329683
60: 3582² (12830724) == 6415361 + 6415363
61: 3738² (13972644) == 6986321 + 6986323
62: 3786² (14333796) == 7166897 + 7166899
63: 3936² (15492096) == 7746047 + 7746049
64: 3948² (15586704) == 7793351 + 7793353
65: 4032² (16257024) == 8128511 + 8128513
66: 4170² (17388900) == 8694449 + 8694451
67: 4200² (17640000) == 8819999 + 8820001
68: 4314² (18610596) == 9305297 + 9305299
69: 4416² (19501056) == 9750527 + 9750529
0.027 seconds elapsed

4288: 999684² (999368099856) == 499684049927 + 499684049929
29996: 9999450² (99989000302500) == 49994500151249 + 49994500151251

71.494 total seconds elapsed

```