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; } }






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