sourcelibs/Numbers/demos/primes.xtl
1⍝!/usr/bin/env xetal
2⍝# Primes: twin primes, Goldbach's conjecture and perfect numbers,
3⍝# each one array expression. Run with "just demo Numbers primes".
4
5ⁿ⁼u̲se< "Numbers"
6p ← ⁿp̲rimes 200
7
8⍝ Twin primes: primes two apart.
9twin ← (2 = (1 d̲rop p) − -1 d̲rop p) r̲eplicate -1 d̲rop p
10twin
11
12⍝ Goldbach: every even number from 4 to 40 as a sum of two primes;
13⍝ here, how many ways (p <= q).
14ᵘg̲oldbach ← { e → q ← (p ≤ e d̲iv 2) r̲eplicate p◆ t̲ally w̲here ⁿp̲rime? e − q }
15'ᵘg̲oldbach e̲ach 2 × 2 d̲rop r̲ange 20
16
17⍝ Perfect numbers below 1000: the sum of the proper divisors is the number.
18v ← 1 d̲rop r̲ange 1000
19s ← '{ ('+ r̲/ ⁿd̲ivisors ⍵) − ⍵ } e̲ach v
20(s = v) r̲eplicate v