WeixiangYan/CodeTransOcean huggingface dataset:
Task: Range extraction







Python: def range_extract(lst):
    'Yield 2-tuple ranges or 1-tuple single elements from list of increasing ints'
    lenlst = len(lst)
    i = 0
    while i< lenlst:
        low = lst[i]
        while i <lenlst-1 and lst[i]+1 == lst[i+1]: i +=1
        hi = lst[i]
        if   hi - low >= 2:
            yield (low, hi)
        elif hi - low == 1:
            yield (low,)
            yield (hi,)
        else:
            yield (low,)
        i += 1

def printr(ranges):
    print( ','.join( (('%i-%i' % r) if len(r) == 2 else '%i' % r)
                     for r in ranges ) )

if __name__ == '__main__':
    for lst in [[-8, -7, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7,
                 8, 9, 10, 11, 14, 15, 17, 18, 19, 20],
                [0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39]]:
        
        printr(range_extract(lst))
Task: Range extraction








VB: Public Function RangeExtraction(AList) As String

Const RangeDelim = "-"          
Dim result As String
Dim InRange As Boolean
Dim Posn, ub, lb, rangestart, rangelen As Integer

result = ""

ub = UBound(AList)
lb = LBound(AList)
Posn = lb
While Posn < ub
  rangestart = Posn
  rangelen = 0
  InRange = True
  
  While InRange
    rangelen = rangelen + 1
    If Posn = ub Then
      InRange = False
    Else
      InRange = (AList(Posn + 1) = AList(Posn) + 1)
      Posn = Posn + 1
    End If
  Wend
  If rangelen > 2 Then 
    result = result & "," & Format$(AList(rangestart)) & RangeDelim & Format$(AList(rangestart + rangelen - 1))
  Else 
    For i = rangestart To rangestart + rangelen - 1
      result = result & "," & Format$(AList(i))
    Next
  End If
  Posn = rangestart + rangelen
Wend
RangeExtraction = Mid$(result, 2) 
End Function


Public Sub RangeTest()


Dim MyList As Variant
MyList = Array(0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39)
Debug.Print "a) "; RangeExtraction(MyList)


Dim MyOtherList(1 To 20) As Integer
MyOtherList(1) = -6
MyOtherList(2) = -3
MyOtherList(3) = -2
MyOtherList(4) = -1
MyOtherList(5) = 0
MyOtherList(6) = 1
MyOtherList(7) = 3
MyOtherList(8) = 4
MyOtherList(9) = 5
MyOtherList(10) = 7
MyOtherList(11) = 8
MyOtherList(12) = 9
MyOtherList(13) = 10
MyOtherList(14) = 11
MyOtherList(15) = 14
MyOtherList(16) = 15
MyOtherList(17) = 17
MyOtherList(18) = 18
MyOtherList(19) = 19
MyOtherList(20) = 20
Debug.Print "b) "; RangeExtraction(MyOtherList)
End Sub
Task: Range extraction




Go: package main

import (
    "errors"
    "fmt"
    "strconv"
    "strings"
)

func main() {
    rf, err := rangeFormat([]int{
        0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
        25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
        37, 38, 39,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("range format:", rf)
}

func rangeFormat(a []int) (string, error) {
    if len(a) == 0 {
        return "", nil
    }
    var parts []string
    for n1 := 0; ; {
        n2 := n1 + 1
        for n2 < len(a) && a[n2] == a[n2-1]+1 {
            n2++
        }
        s := strconv.Itoa(a[n1])
        if n2 == n1+2 {
            s += "," + strconv.Itoa(a[n2-1])
        } else if n2 > n1+2 {
            s += "-" + strconv.Itoa(a[n2-1])
        }
        parts = append(parts, s)
        if n2 == len(a) {
            break
        }
        if a[n2] == a[n2-1] {
            return "", errors.New(fmt.Sprintf(
                "sequence repeats value %d", a[n2]))
        }
        if a[n2] < a[n2-1] {
            return "", errors.New(fmt.Sprintf(
                "sequence not ordered: %d < %d", a[n2], a[n2-1]))
        }
        n1 = n2
    }
    return strings.Join(parts, ","), nil
}
Task: Range extraction


C++: #include <iostream>
#include <iterator>
#include <cstddef>

template<typename InIter>
 void extract_ranges(InIter begin, InIter end, std::ostream& os)
{
  if (begin == end)
    return;

  int current = *begin++;
  os << current;
  int count = 1;

  while (begin != end)
  {
    int next = *begin++;
    if (next == current+1)
      ++count;
    else
    {
      if (count > 2)
        os << '-';
      else
        os << ',';
      if (count > 1)
        os << current << ',';
      os << next;
      count = 1;
    }
    current = next;
  }

  if (count > 1)
    os << (count > 2? '-' : ',') << current;
}

template<typename T, std::size_t n>
 T* end(T (&array)[n])
{
  return array+n;
}

int main()
{
  int data[] = { 0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
                 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                 25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
                 37, 38, 39 };

  extract_ranges(data, end(data), std::cout);
  std::cout << std::endl;
}









Fortran:       SUBROUTINE IRANGE(TEXT)	
Could make this a function, but then a maximum text length returned would have to be specified.
       CHARACTER*(*) TEXT	
       INTEGER LOTS		
       PARAMETER (LOTS = 666)	
       INTEGER VAL(LOTS)	
       INTEGER N		
       INTEGER I,I1		
        N = 1		
        DO I = 1,LEN(TEXT)	
          IF (TEXT(I:I).EQ.",") N = N + 1	
        END DO			
        IF (N.LE.2) RETURN	
        IF (N.GT.LOTS) STOP "Too many values
        READ (TEXT,*) VAL(1:N)	
        TEXT = ""		
        L = 0			
        I1 = 1			
   10   IF (L.GT.0) CALL EMIT(",")	
        CALL SPLOT(VAL(I1))		
        DO I = I1 + 1,N			
          IF (VAL(I - 1) + 1 .NE. VAL(I)) EXIT	
        END DO				
        IF (I - I1 .GT. 2) THEN		
          CALL EMIT("-")		
          CALL SPLOT(VAL(I - 1))	
          I1 = I			
         ELSE			
          I1 = I1 + 1			
        END IF			
        IF (I.LE.N) GO TO 10	
       CONTAINS		
        SUBROUTINE EMIT(C)	
         CHARACTER*1 C		
          L = L + 1		
          IF (L.GT.LEN(TEXT)) STOP "Ran out of text
          TEXT(L:L) = C		
        END SUBROUTINE EMIT	
        SUBROUTINE SPLOT(N)	
         INTEGER N		
         CHARACTER*12 FIELD	
         INTEGER I		
          WRITE (FIELD,"(I0)") N
          DO I = 1,12		
            IF (FIELD(I:I).LE." ") EXIT	
            CALL EMIT(FIELD(I:I))	
          END DO		
        END SUBROUTINE SPLOT	
      END	

      PROGRAM POKE
      CHARACTER*(200) SOME
      SOME = "  0,  1,  2,  4,  6,  7,  8, 11, 12, 14,  "
     1      //"  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,"
     2      //"25, 27, 28, 29, 30, 31, 32, 33, 35, 36,  "
     3      //"37, 38, 39                               "
      CALL IRANGE(SOME)
      WRITE (6,*) SOME
      END
Task: Range extraction

C: #include <stdio.h>
#include <stdlib.h>

size_t rprint(char *s, int *x, int len)
{
#define sep (a > s ? "," : "") 
#define ol (s ? 100 : 0)       
	int i, j;
	char *a = s;
	for (i = j = 0; i < len; i = ++j) {
		for (; j < len - 1 && x[j + 1] == x[j] + 1; j++);

		if (i + 1 < j)
			a += snprintf(s?a:s, ol, "%s%d-%d", sep, x[i], x[j]);
		else
			while (i <= j)
				a += snprintf(s?a:s, ol, "%s%d", sep, x[i++]);
	}
	return a - s;
#undef sep
#undef ol
}

int main()
{
	int x[] = {	0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
			15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
			25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
			37, 38, 39 };

	char *s = malloc(rprint(0, x, sizeof(x) / sizeof(int)) + 1);
	rprint(s, x, sizeof(x) / sizeof(int));
	printf("%s\n", s);

	return 0;
}










Fortran:       SUBROUTINE IRANGE(TEXT)	
Could make this a function, but then a maximum text length returned would have to be specified.
       CHARACTER*(*) TEXT	
       INTEGER LOTS		
       PARAMETER (LOTS = 666)	
       INTEGER VAL(LOTS)	
       INTEGER N		
       INTEGER I,I1		
        N = 1		
        DO I = 1,LEN(TEXT)	
          IF (TEXT(I:I).EQ.",") N = N + 1	
        END DO			
        IF (N.LE.2) RETURN	
        IF (N.GT.LOTS) STOP "Too many values
        READ (TEXT,*) VAL(1:N)	
        TEXT = ""		
        L = 0			
        I1 = 1			
   10   IF (L.GT.0) CALL EMIT(",")	
        CALL SPLOT(VAL(I1))		
        DO I = I1 + 1,N			
          IF (VAL(I - 1) + 1 .NE. VAL(I)) EXIT	
        END DO				
        IF (I - I1 .GT. 2) THEN		
          CALL EMIT("-")		
          CALL SPLOT(VAL(I - 1))	
          I1 = I			
         ELSE			
          I1 = I1 + 1			
        END IF			
        IF (I.LE.N) GO TO 10	
       CONTAINS		
        SUBROUTINE EMIT(C)	
         CHARACTER*1 C		
          L = L + 1		
          IF (L.GT.LEN(TEXT)) STOP "Ran out of text
          TEXT(L:L) = C		
        END SUBROUTINE EMIT	
        SUBROUTINE SPLOT(N)	
         INTEGER N		
         CHARACTER*12 FIELD	
         INTEGER I		
          WRITE (FIELD,"(I0)") N
          DO I = 1,12		
            IF (FIELD(I:I).LE." ") EXIT	
            CALL EMIT(FIELD(I:I))	
          END DO		
        END SUBROUTINE SPLOT	
      END	

      PROGRAM POKE
      CHARACTER*(200) SOME
      SOME = "  0,  1,  2,  4,  6,  7,  8, 11, 12, 14,  "
     1      //"  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,"
     2      //"25, 27, 28, 29, 30, 31, 32, 33, 35, 36,  "
     3      //"37, 38, 39                               "
      CALL IRANGE(SOME)
      WRITE (6,*) SOME
      END
Task: Range extraction





Java: public class RangeExtraction {

    public static void main(String[] args) {
        int[] arr = {0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
            25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
            37, 38, 39};

        int len = arr.length;
        int idx = 0, idx2 = 0;
        while (idx < len) {
            while (++idx2 < len && arr[idx2] - arr[idx2 - 1] == 1);
            if (idx2 - idx > 2) {
                System.out.printf("%s-%s,", arr[idx], arr[idx2 - 1]);
                idx = idx2;
            } else {
                for (; idx < idx2; idx++)
                    System.out.printf("%s,", arr[idx]);
            }
        }
    }
}






Fortran:       SUBROUTINE IRANGE(TEXT)	
Could make this a function, but then a maximum text length returned would have to be specified.
       CHARACTER*(*) TEXT	
       INTEGER LOTS		
       PARAMETER (LOTS = 666)	
       INTEGER VAL(LOTS)	
       INTEGER N		
       INTEGER I,I1		
        N = 1		
        DO I = 1,LEN(TEXT)	
          IF (TEXT(I:I).EQ.",") N = N + 1	
        END DO			
        IF (N.LE.2) RETURN	
        IF (N.GT.LOTS) STOP "Too many values
        READ (TEXT,*) VAL(1:N)	
        TEXT = ""		
        L = 0			
        I1 = 1			
   10   IF (L.GT.0) CALL EMIT(",")	
        CALL SPLOT(VAL(I1))		
        DO I = I1 + 1,N			
          IF (VAL(I - 1) + 1 .NE. VAL(I)) EXIT	
        END DO				
        IF (I - I1 .GT. 2) THEN		
          CALL EMIT("-")		
          CALL SPLOT(VAL(I - 1))	
          I1 = I			
         ELSE			
          I1 = I1 + 1			
        END IF			
        IF (I.LE.N) GO TO 10	
       CONTAINS		
        SUBROUTINE EMIT(C)	
         CHARACTER*1 C		
          L = L + 1		
          IF (L.GT.LEN(TEXT)) STOP "Ran out of text
          TEXT(L:L) = C		
        END SUBROUTINE EMIT	
        SUBROUTINE SPLOT(N)	
         INTEGER N		
         CHARACTER*12 FIELD	
         INTEGER I		
          WRITE (FIELD,"(I0)") N
          DO I = 1,12		
            IF (FIELD(I:I).LE." ") EXIT	
            CALL EMIT(FIELD(I:I))	
          END DO		
        END SUBROUTINE SPLOT	
      END	

      PROGRAM POKE
      CHARACTER*(200) SOME
      SOME = "  0,  1,  2,  4,  6,  7,  8, 11, 12, 14,  "
     1      //"  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,"
     2      //"25, 27, 28, 29, 30, 31, 32, 33, 35, 36,  "
     3      //"37, 38, 39                               "
      CALL IRANGE(SOME)
      WRITE (6,*) SOME
      END
Task: Range extraction







Python: def range_extract(lst):
    'Yield 2-tuple ranges or 1-tuple single elements from list of increasing ints'
    lenlst = len(lst)
    i = 0
    while i< lenlst:
        low = lst[i]
        while i <lenlst-1 and lst[i]+1 == lst[i+1]: i +=1
        hi = lst[i]
        if   hi - low >= 2:
            yield (low, hi)
        elif hi - low == 1:
            yield (low,)
            yield (hi,)
        else:
            yield (low,)
        i += 1

def printr(ranges):
    print( ','.join( (('%i-%i' % r) if len(r) == 2 else '%i' % r)
                     for r in ranges ) )

if __name__ == '__main__':
    for lst in [[-8, -7, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7,
                 8, 9, 10, 11, 14, 15, 17, 18, 19, 20],
                [0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39]]:
        
        printr(range_extract(lst))




Fortran:       SUBROUTINE IRANGE(TEXT)	
Could make this a function, but then a maximum text length returned would have to be specified.
       CHARACTER*(*) TEXT	
       INTEGER LOTS		
       PARAMETER (LOTS = 666)	
       INTEGER VAL(LOTS)	
       INTEGER N		
       INTEGER I,I1		
        N = 1		
        DO I = 1,LEN(TEXT)	
          IF (TEXT(I:I).EQ.",") N = N + 1	
        END DO			
        IF (N.LE.2) RETURN	
        IF (N.GT.LOTS) STOP "Too many values
        READ (TEXT,*) VAL(1:N)	
        TEXT = ""		
        L = 0			
        I1 = 1			
   10   IF (L.GT.0) CALL EMIT(",")	
        CALL SPLOT(VAL(I1))		
        DO I = I1 + 1,N			
          IF (VAL(I - 1) + 1 .NE. VAL(I)) EXIT	
        END DO				
        IF (I - I1 .GT. 2) THEN		
          CALL EMIT("-")		
          CALL SPLOT(VAL(I - 1))	
          I1 = I			
         ELSE			
          I1 = I1 + 1			
        END IF			
        IF (I.LE.N) GO TO 10	
       CONTAINS		
        SUBROUTINE EMIT(C)	
         CHARACTER*1 C		
          L = L + 1		
          IF (L.GT.LEN(TEXT)) STOP "Ran out of text
          TEXT(L:L) = C		
        END SUBROUTINE EMIT	
        SUBROUTINE SPLOT(N)	
         INTEGER N		
         CHARACTER*12 FIELD	
         INTEGER I		
          WRITE (FIELD,"(I0)") N
          DO I = 1,12		
            IF (FIELD(I:I).LE." ") EXIT	
            CALL EMIT(FIELD(I:I))	
          END DO		
        END SUBROUTINE SPLOT	
      END	

      PROGRAM POKE
      CHARACTER*(200) SOME
      SOME = "  0,  1,  2,  4,  6,  7,  8, 11, 12, 14,  "
     1      //"  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,"
     2      //"25, 27, 28, 29, 30, 31, 32, 33, 35, 36,  "
     3      //"37, 38, 39                               "
      CALL IRANGE(SOME)
      WRITE (6,*) SOME
      END
Task: Copy stdin to stdout



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        Console.OpenStandardInput().CopyTo(Console.OpenStandardOutput());
    }
}









Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout

C: #include <stdio.h>

int main(){
  char c;
  while ( (c=getchar()) != EOF ){
    putchar(c);
  }
  return 0;
}











Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout


C++: #include <iostream>
#include <iterator>

int main() {
    using namespace std;
    noskipws(cin);
    copy(
        istream_iterator<char>(cin),
        istream_iterator<char>(),
        ostream_iterator<char>(cout)
    );
    return 0;
}










Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout




Go: package main

import (
    "bufio"
    "io"
    "os"
)

func main() {
    r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    for {
        b, err := r.ReadByte()       
        if err == io.EOF {
            return
        }
        w.WriteByte(b)
        w.Flush()
    }   
}








Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout





Java: import java.util.Scanner;

public class CopyStdinToStdout {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in);) {
            String s;
            while ( (s = scanner.nextLine()).compareTo("") != 0 ) {
                System.out.println(s);
            }
        }
    }

}







Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout








VB: do
  s=wscript.stdin.readline
  wscript.stdout.writeline s 
loop until asc(left(s,1))=26




Ada: with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is
   use Ada.Text_IO;
   C : Character;
begin
   while not End_Of_File loop
      Get_Immediate (C);
      Put (C);
   end loop;
end Copy_Stdin_To_Stdout;
Task: Copy stdin to stdout

C: #include <stdio.h>

int main(){
  char c;
  while ( (c=getchar()) != EOF ){
    putchar(c);
  }
  return 0;
}
Task: Copy stdin to stdout



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        Console.OpenStandardInput().CopyTo(Console.OpenStandardOutput());
    }
}
Task: Copy stdin to stdout


C++: #include <iostream>
#include <iterator>

int main() {
    using namespace std;
    noskipws(cin);
    copy(
        istream_iterator<char>(cin),
        istream_iterator<char>(),
        ostream_iterator<char>(cout)
    );
    return 0;
}
Task: Copy stdin to stdout





Java: import java.util.Scanner;

public class CopyStdinToStdout {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in);) {
            String s;
            while ( (s = scanner.nextLine()).compareTo("") != 0 ) {
                System.out.println(s);
            }
        }
    }

}
Task: Copy stdin to stdout








VB: do
  s=wscript.stdin.readline
  wscript.stdout.writeline s 
loop until asc(left(s,1))=26
Task: Copy stdin to stdout




Go: package main

import (
    "bufio"
    "io"
    "os"
)

func main() {
    r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    for {
        b, err := r.ReadByte()       
        if err == io.EOF {
            return
        }
        w.WriteByte(b)
        w.Flush()
    }   
}
Task: Zeckendorf number representation



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

namespace Zeckendorf
{
    class Program
    {
        private static uint Fibonacci(uint n)
        {
            if (n < 2)
            {
                return n;
            }
            else
            {
                return Fibonacci(n - 1) + Fibonacci(n - 2);
            }
        }

        private static string Zeckendorf(uint num)
        {
            IList<uint> fibonacciNumbers = new List<uint>();
            uint fibPosition = 2;

            uint currentFibonaciNum = Fibonacci(fibPosition);

            do
            {
                fibonacciNumbers.Add(currentFibonaciNum);
                currentFibonaciNum = Fibonacci(++fibPosition);
            } while (currentFibonaciNum <= num);

            uint temp = num;
            StringBuilder output = new StringBuilder();

            foreach (uint item in fibonacciNumbers.Reverse())
            {
                if (item <= temp)
                {
                    output.Append("1");
                    temp -= item;
                }
                else
                {
                    output.Append("0");
                }
            }

            return output.ToString();
        }

        static void Main(string[] args)
        {
            for (uint i = 1; i <= 20; i++)
            {
                string zeckendorfRepresentation = Zeckendorf(i);
                Console.WriteLine(string.Format("{0} : {1}", i, zeckendorfRepresentation));
            }

            Console.ReadKey();
        }
    }
}









Ada: with Ada.Text_IO, Ada.Strings.Unbounded;

procedure Print_Zeck is
   
   function Zeck_Increment(Z: String) return String is
   begin
      if Z="" then 
	 return "1";
      elsif Z(Z'Last) = '1' then
	 return Zeck_Increment(Z(Z'First .. Z'Last-1)) & '0';
      elsif Z(Z'Last-1) = '0' then
	 return Z(Z'First .. Z'Last-1) & '1';
      else 
	 return Zeck_Increment(Z(Z'First .. Z'Last-2)) & "00";
      end if;
   end Zeck_Increment;
   
   use Ada.Strings.Unbounded;
   Current: Unbounded_String := Null_Unbounded_String;
   
begin
   for I in 1 .. 20 loop
      Current := To_Unbounded_String(Zeck_Increment(To_String(Current)));
      Ada.Text_IO.Put(To_String(Current) & " ");
   end loop;   
end Print_Zeck;
Task: Zeckendorf number representation

C: #include <stdio.h>

typedef unsigned long long u64;

#define FIB_INVALID (~(u64)0)

u64 fib[] = {
	1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,
	2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,
	317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465,
	14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
	267914296, 433494437, 701408733, 1134903170, 1836311903,
	2971215073ULL, 4807526976ULL, 7778742049ULL, 12586269025ULL,
	20365011074ULL, 32951280099ULL, 53316291173ULL, 86267571272ULL,
	139583862445ULL, 225851433717ULL, 365435296162ULL, 591286729879ULL,
	956722026041ULL, 1548008755920ULL, 2504730781961ULL, 4052739537881ULL,
	6557470319842ULL, 10610209857723ULL, 17167680177565ULL,

	27777890035288ULL 
};

u64 fibbinary(u64 n)
{
	if (n >= fib[64]) return FIB_INVALID;

	u64 ret = 0;
	int i;
	for (i = 64; i--; )
		if (n >= fib[i]) {
			ret |= 1ULL << i;
			n -= fib[i];
		}

	return ret;
}

void bprint(u64 n, int width)
{
	if (width > 64) width = 64;

	u64 b;
	for (b = 1ULL << (width - 1); b; b >>= 1)
		putchar(b == 1 && !n
			? '0'
			: b > n	? ' '
				: b & n ? '1' : '0');
	putchar('\n');
}

int main(void)
{
	int i;

	for (i = 0; i <= 20; i++)
		printf("%2d:", i), bprint(fibbinary(i), 8);

	return 0;
}











Ada: with Ada.Text_IO, Ada.Strings.Unbounded;

procedure Print_Zeck is
   
   function Zeck_Increment(Z: String) return String is
   begin
      if Z="" then 
	 return "1";
      elsif Z(Z'Last) = '1' then
	 return Zeck_Increment(Z(Z'First .. Z'Last-1)) & '0';
      elsif Z(Z'Last-1) = '0' then
	 return Z(Z'First .. Z'Last-1) & '1';
      else 
	 return Zeck_Increment(Z(Z'First .. Z'Last-2)) & "00";
      end if;
   end Zeck_Increment;
   
   use Ada.Strings.Unbounded;
   Current: Unbounded_String := Null_Unbounded_String;
   
begin
   for I in 1 .. 20 loop
      Current := To_Unbounded_String(Zeck_Increment(To_String(Current)));
      Ada.Text_IO.Put(To_String(Current) & " ");
   end loop;   
end Print_Zeck;