One-two primes

say "Smallest n digit prime using only 1 and 2:"

for k in (0..20) {
    ['1','2'].variations_with_repetition(k, {|*a|
        var p = Num(a.join)
        if (p.is_prime) {
            printf("%4d. %s\n", k, p)
            break
        }
    })
}

for k in (100..2000 `by` 100) {
    ['1','2'].variations_with_repetition(k-1, {|*a|
        var p = Num(a.join + '1')
        if (p.is_prime) {
            var t = Str(p)
            var i = t.index('2')
            printf("%4d. (1 x %s) %s\n", k, i, t.substr(i))
            break
        }
    })
}

Output:

Smallest n digit prime using only 1 and 2:
   1. 2
   2. 11
   3. 211
   4. 2111
   5. 12211
   6. 111121
   7. 1111211
   8. 11221211
   9. 111112121
  10. 1111111121
  11. 11111121121
  12. 111111211111
  13. 1111111121221
  14. 11111111112221
  15. 111111112111121
  16. 1111111112122111
  17. 11111111111112121
  18. 111111111111112111
  19. 1111111111111111111
  20. 11111111111111212121
 100. (1 x 92) 21112211
 200. (1 x 192) 21112211
 300. (1 x 288) 211121112221
 400. (1 x 390) 2111122121
 500. (1 x 488) 221222111111
 600. (1 x 590) 2112222221
 700. (1 x 689) 21111111111
 800. (1 x 787) 2122222221111
 900. (1 x 891) 222221221
1000. (1 x 988) 222122111121
1100. (1 x 1087) 2112111121111
1200. (1 x 1191) 211222211
1300. (1 x 1289) 22121221121
1400. (1 x 1388) 222211222121
1500. (1 x 1489) 21112121121
1600. (1 x 1587) 2121222122111
1700. (1 x 1688) 212121211121
1800. (1 x 1791) 221211121
1900. (1 x 1889) 22212212211
2000. (1 x 1989) 22121121211

Last updated