Task: Primes with digits in nondecreasing order



C#: using System.Linq; using System.Collections.Generic; using static System.Console; using static System.Math;
 
class Program {

  static int ba; static string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  
  static string from10(int b) { string res = ""; int re; while (b > 0) {
    b = DivRem(b, ba, out re); res = chars[(byte)re] + res; } return res; }

  
  static int to10(string s) { int res = 0; foreach (char i in s)
    res = res * ba + chars.IndexOf(i); return res; }

  
  static bool nd(string s) { if (s.Length < 2) return true;
    char l = s[0]; for (int i = 1; i < s.Length; i++)
      if (chars.IndexOf(l) > chars.IndexOf(s[i]))
        return false; else l = s[i] ; return true; }

  static void Main(string[] args) { int c, lim = 1000; string s;
    foreach (var b in new List<int>{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 27, 31, 62 }) {
      ba = b; c = 0; foreach (var a in PG.Primes(lim))
        if (nd(s = from10(a))) Write("{0,4} {1}", s, ++c % 20 == 0 ? "\n" : "");
    WriteLine("\nBase {0}: found {1} non-decreasing primes under {2:n0}\n", b, c, from10(lim)); } } } 
 
class PG { public static IEnumerable<int> Primes(int lim) {
    var flags = new bool[lim + 1]; int j; yield return 2;
    for (j = 4; j <= lim; j += 2) flags[j] = true; j = 3;
    for (int d = 8, sq = 9; sq <= lim; j += 2, sq += d += 8)
      if (!flags[j]) { yield return j;
        for (int k = sq, i = j << 1; k <= lim; k += i) flags[k] = true; }
    for (; j <= lim; j += 2) if (!flags[j]) yield return j; } }





Python: 

from operator import le
from itertools import takewhile



def monotonicDigits(base):
    
    def go(n):
        return monotonic(le)(
            showIntAtBase(base)(digitFromInt)(n)('')
        )
    return go



def monotonic(op):
    
    def go(xs):
        return all(map(op, xs, xs[1:]))
    return go




def main():
    
    xs = [
        str(n) for n in takewhile(
            lambda n: 1000 > n,
            filter(monotonicDigits(10), primes())
        )
    ]
    w = len(xs[-1])
    print(f'{len(xs)} matches for base 10:\n')
    print('\n'.join(
        ' '.join(row) for row in chunksOf(10)([
            x.rjust(w, ' ') for x in xs
        ])
    ))





def chunksOf(n):
    
    def go(xs):
        return (
            xs[i:n + i] for i in range(0, len(xs), n)
        ) if 0 < n else None
    return go



def digitFromInt(n):
    
    return '0123456789abcdefghijklmnopqrstuvwxyz'[n] if (
        0 <= n < 36
    ) else '?'



def primes():
    
    n = 2
    dct = {}
    while True:
        if n in dct:
            for p in dct[n]:
                dct.setdefault(n + p, []).append(p)
            del dct[n]
        else:
            yield n
            dct[n * n] = [n]
        n = 1 + n




def showIntAtBase(base):
    
    def wrap(toChr, n, rs):
        def go(nd, r):
            n, d = nd
            r_ = toChr(d) + r
            return go(divmod(n, base), r_) if 0 != n else r_
        return 'unsupported base' if 1 >= base else (
            'negative number' if 0 > n else (
                go(divmod(n, base), rs))
        )
    return lambda toChr: lambda n: lambda rs: (
        wrap(toChr, n, rs)
    )



if __name__ == '__main__':
    main()