Task: Van der Corput sequence



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VanDerCorput
{
    
    
    
    
    
    
    
    
    
    
    
    
    public class VanDerCorputSequence: IEnumerable<Tuple<long,long>>
    {
        
        
        
        public int Base { get; private set; }

        
        
        
        public long Count { get; private set; }

        
        
        
        
        
        public VanDerCorputSequence(int iBase, long count = long.MaxValue) {
            if (iBase < 2)
                throw new ArgumentOutOfRangeException("iBase", "must be two or greater, not the given value of " + iBase);
            Base = iBase;
            Count = count;
        }

        
        
        
        
        
        
        public Tuple<long,long> Compute(long n)
        {
            long p = 0, q = 1;
            long numerator, denominator;
            while (n != 0)
            {
                p = p * Base + (n % Base);
                q *= Base;
                n /= Base;
            }
            numerator = p;
            denominator = q;
            while (p != 0) 
            { 
                n = p; 
                p = q % p; 
                q = n; 
            }
            numerator /= q;
            denominator /= q;
            return new Tuple<long,long>(numerator, denominator);
        }

        
        
        
        
        
        
        public static Tuple<long, long> Compute(int iBase, long n)
        {
            var seq = new VanDerCorputSequence(iBase);
            return seq.Compute(n);
        }

        
        
        
        
        
        public IEnumerator<Tuple<long, long>> GetEnumerator()
        {
            long iSequenceIndex = 0L;
            while (iSequenceIndex < Count)
            {
                yield return Compute(iSequenceIndex);
                iSequenceIndex++;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TestBasesTwoThroughFive();

            Console.WriteLine("Type return to continue...");
            Console.ReadLine();
        }

        static void TestBasesTwoThroughFive()
        {
            foreach (var seq in Enumerable.Range(2, 5).Select(x => new VanDerCorputSequence(x, 10))) 
            {
                Console.Write("base " + seq.Base + ":");
                foreach(var vc in seq) 
                    Console.Write(" " + vc.Item1 + "/" + vc.Item2);
                Console.WriteLine();
            }
        }
    }
}








Fortran:       FUNCTION VDC(N,BASE)	
Converts 1234 in decimal to 4321 in V, and P = 10000.
       INTEGER N	
       INTEGER BASE	
       INTEGER I	
       INTEGER P	
       INTEGER V	
        P = 1		
        V = 0		
        I = N		
        DO WHILE (I .NE. 0)	
          V = V*BASE + MOD(I,BASE)	
          I = I/BASE			
          P = P*BASE			
        END DO			
        VDC = V/FLOAT(P)	
      END FUNCTION VDC	

      PROGRAM POKE
      INTEGER FIRST,LAST	
      PARAMETER (FIRST = 0,LAST = 9)	
      INTEGER I,BASE		
      REAL VDC			

      WRITE (6,1) FIRST,LAST,(I, I = FIRST,LAST)	
    1 FORMAT ("Calculates values ",I0," to ",I0," of the ",
     1 "Van der Corput sequence, in various bases."/
     2 "Base",666I9)

      DO BASE = 2,13	
        WRITE (6,2) BASE,(VDC(I,BASE), I = FIRST,LAST)	
    2   FORMAT (I4,666F9.6)	
      END DO		

      END