Task: Brazilian numbers







Python: 

from itertools import count, islice



def isBrazil(n):
    
    return 7 <= n and (
        0 == n % 2 or any(
            map(monoDigit(n), range(2, n - 1))
        )
    )



def monoDigit(n):
    
    def go(base):
        def g(b, n):
            (q, d) = divmod(n, b)

            def p(qr):
                return d != qr[1] or 0 == qr[0]

            def f(qr):
                return divmod(qr[0], b)
            return d == until(p)(f)(
                (q, d)
            )[1]
        return g(base, n)
    return go




def main():
    
    for kxs in ([
            (' ', count(1)),
            (' odd ', count(1, 2)),
            (' prime ', primes())
    ]):
        print(
            'First 20' + kxs[0] + 'Brazilians:\n' +
            showList(take(20)(filter(isBrazil, kxs[1]))) + '\n'
        )





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 showList(xs):
    
    return '[' + ','.join(str(x) for x in xs) + ']'




def take(n):
    
    def go(xs):
        return (
            xs[0:n]
            if isinstance(xs, (list, tuple))
            else list(islice(xs, n))
        )
    return go



def until(p):
    
    def go(f):
        def g(x):
            v = x
            while not p(v):
                v = f(v)
            return v
        return g
    return go



if __name__ == '__main__':
    main()




Fortran: 

      PROGRAM BRAZILIAN
      IMPLICIT NONE



      INTEGER , PARAMETER  ::  MAX_NUMBER = 2000000 , NUMVARS = 20



      LOGICAL , DIMENSION(1:MAX_NUMBER)  ::  b
      INTEGER  ::  bcount
      INTEGER  ::  bpos
      CHARACTER(15)  ::  holder
      CHARACTER(100)  ::  outline
      LOGICAL , DIMENSION(1:MAX_NUMBER)  ::  p






      CALL BRAZILIANSIEVE(b , MAX_NUMBER)
      WRITE(6 , 34)"The first 20 Brazilian numbers:"
      bcount = 0
      outline = ''
      holder = ''
      bpos = 1
      DO WHILE ( bcount<NUMVARS )
         IF( b(bpos) )THEN
            bcount = bcount + 1
            WRITE(holder , *)bpos
            outline = TRIM(outline) // " " // ADJUSTL(holder)
         END IF
         bpos = bpos + 1
      END DO
 
      WRITE(6 , 34)outline
      WRITE(6 , 34)"The first 20 odd Brazilian numbers:"
      outline = ''
      holder = ''
      bcount = 0
      bpos = 1
      DO WHILE ( bcount<NUMVARS )
         IF( b(bpos) )THEN
            bcount = bcount + 1
            WRITE(holder , *)bpos
            outline = TRIM(outline) // " " // ADJUSTL(holder)
         END IF
         bpos = bpos + 2
      END DO
      WRITE(6 , 34)outline
      WRITE(6 , 34)"The first 20 prime Brazilian numbers:"
      CALL ERATOSTHENES(p , MAX_NUMBER)
      bcount = 0
      outline = ''
      holder = ''
      bpos = 1
      DO WHILE ( bcount<NUMVARS )
         IF( b(bpos) .AND. p(bpos) )THEN
            bcount = bcount + 1
            WRITE(holder , *)bpos
            outline = TRIM(outline) // " " // ADJUSTL(holder)
         END IF
         bpos = bpos + 1
      END DO
      WRITE(6 , 34)outline
      WRITE(6 , 34)"Various Brazilian numbers:"
      bcount = 0
      bpos = 1
      DO WHILE ( bcount<1000000 )
         IF( b(bpos) )THEN
            bcount = bcount + 1
            IF( (bcount==100) .OR. (bcount==1000) .OR. (bcount==10000) .OR.               &
              & (bcount==100000) .OR. (bcount==1000000) )WRITE(* , *)bcount ,             &
               &"th Brazilian number: " , bpos
         END IF
         bpos = bpos + 1
      END DO
      STOP
 34   FORMAT(/ , a)
      END PROGRAM BRAZILIAN

      SUBROUTINE BRAZILIANSIEVE(B , N)
      IMPLICIT NONE



      INTEGER  ::  N
      LOGICAL , DIMENSION(*)  ::  B
      INTENT (IN) N
      INTENT (OUT) B



      INTEGER  ::  b11
      INTEGER  ::  base
      INTEGER  ::  bn
      INTEGER  ::  bnn
      INTEGER  ::  bpower
      INTEGER  ::  digit
      INTEGER  ::  i
      LOGICAL  ::  iseven
      INTEGER  ::  powermax

      iseven = .FALSE.
      B(1:6) = .FALSE.                 
      DO i = 7 , N
         B(i) = iseven
         iseven = .NOT.iseven
      END DO
      DO base = 2 , (N/2)
         b11 = base + 1
         bnn = b11
         DO digit = 3 , base - 1 , 2
            bnn = bnn + b11 + b11
            IF( bnn>N )EXIT
            B(bnn) = .TRUE.
         END DO
      END DO
      DO base = 2 , INT(SQRT(FLOAT(N)))
         powermax = HUGE(powermax)/base             
         IF( powermax>N )powermax = N               
         DO digit = 1 , base - 1 , 2
            bpower = base*base
            bn = digit*(bpower + base + 1)
            DO WHILE ( (bn<=N) .AND. (bpower<=powermax) )
               IF( bn<=N )B(bn) = .TRUE.
               bpower = bpower*base
               bn = bn + (digit*bpower)
            END DO
         END DO
      END DO
      RETURN
      END SUBROUTINE BRAZILIANSIEVE

      SUBROUTINE ERATOSTHENES(P , N)
      IMPLICIT NONE



      INTEGER  ::  N
      LOGICAL , DIMENSION(*)  ::  P
      INTENT (IN) N
      INTENT (INOUT) P



      INTEGER  ::  i
      INTEGER  ::  ii
      LOGICAL  ::  oddeven
      INTEGER  ::  pr

      P(1) = .FALSE.
      P(2) = .TRUE.
      oddeven = .TRUE.
      DO i = 3 , N
         P(i) = oddeven
         oddeven = .NOT.oddeven
      END DO
      DO i = 2 , INT(SQRT(FLOAT(N)))
         ii = i + i
         IF( P(i) )THEN
            DO pr = i*i , N , ii
               P(pr) = .FALSE.
            END DO
         END IF
      END DO
      RETURN
      END SUBROUTINE ERATOSTHENES