Task: Primes with digits in nondecreasing order







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()


VB: Imports System.Linq
Imports System.Collections.Generic
Imports System.Console
Imports System.Math

Module Module1
    Dim ba As Integer
    Dim chars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

    Iterator Function Primes(ByVal lim As Integer) As IEnumerable(Of Integer)
        Dim flags(lim) As Boolean, j As Integer : Yield 2
        For j = 4 To lim Step 2 : flags(j) = True : Next : j = 3
        Dim d As Integer = 8, sq As Integer = 9
        While sq <= lim
            If Not flags(j) Then
                Yield j : Dim i As Integer = j << 1
                For k As Integer = sq To lim step i : flags(k) = True : Next
            End If
            j += 2 : d += 8 : sq += d : End While
        While j <= lim
            If Not flags(j) Then Yield j
            j += 2 : End While
    End Function

    
    Function from10(ByVal b As Integer) As String
        Dim res As String = "", re As Integer
        While b > 0 : b = DivRem(b, ba, re) : res = chars(CByte(re)) & res : End While : Return res
    End Function

    
    Function to10(ByVal s As String) As Integer
        Dim res As Integer = 0
        For Each i As Char In s : res = res * ba + chars.IndexOf(i) : Next : Return res
    End Function

    
    Function nd(ByVal s As String) As Boolean
        If s.Length < 2 Then Return True
        Dim l As Char = s(0)
        For i As Integer = 1 To s.Length - 1
            If chars.IndexOf(l) > chars.IndexOf(s(i)) Then Return False Else l = s(i)
        Next : Return True
    End Function

    Sub Main(ByVal args As String())
        Dim c As Integer, lim As Integer = 1000, s As String
        For Each b As Integer In New List(Of Integer) From { 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 27, 31, 62 }
            ba = b : c = 0 : For Each a As Integer In Primes(lim)
                s = from10(a) : If nd(s) Then c += 1 : Write("{0,4} {1}", s, If(c Mod 20 = 0, vbLf, ""))
            Next
            WriteLine(vbLf & "Base {0}: found {1} non-decreasing primes under {2:n0}" & vbLf, b, c, from10(lim))
        Next
    End Sub
End Module