WeixiangYan/CodeTransOcean huggingface dataset:
Task: Even or odd







Python: >>> def is_odd(i): return bool(i & 1)

>>> def is_even(i): return not is_odd(i)

>>> [(j, is_odd(j)) for j in range(10)]
[(0, False), (1, True), (2, False), (3, True), (4, False), (5, True), (6, False), (7, True), (8, False), (9, True)]
>>> [(j, is_even(j)) for j in range(10)]
[(0, True), (1, False), (2, True), (3, False), (4, True), (5, False), (6, True), (7, False), (8, True), (9, False)]
>>>



COBOL:        IF FUNCTION REM(Num, 2) = 0
           DISPLAY Num " is even."
       ELSE
           DISPLAY Num " is odd."
       END-IF
Task: Even or odd








VB: Option Explicit

Sub Main_Even_Odd()
Dim i As Long

    For i = -50 To 48 Step 7
        Debug.Print i & " : IsEven ==> " & IIf(IsEven(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven2 ==> " & IIf(IsEven2(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven3 ==> " & IIf(IsEven3(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven4 ==> " & IIf(IsEven4(i), "is even", "is odd")
    Next
End Sub

Function IsEven(Number As Long) As Boolean

    IsEven = (WorksheetFunction.Even(Number) = Number)
End Function

Function IsEven2(Number As Long) As Boolean


Dim lngTemp As Long
    lngTemp = CLng(Right(CStr(Number), 1))
    If (lngTemp And 1) = 0 Then IsEven2 = True
End Function

Function IsEven3(Number As Long) As Boolean


Dim sngTemp As Single
    sngTemp = Number / 2
    IsEven3 = ((Int(sngTemp) - sngTemp) = 0)
End Function

Function IsEven4(Number As Long) As Boolean

    IsEven4 = (Number Mod 2 = 0)
End Function


COBOL:        IF FUNCTION REM(Num, 2) = 0
           DISPLAY Num " is even."
       ELSE
           DISPLAY Num " is odd."
       END-IF
Task: Even or odd




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    test(-2)
    test(-1)
    test(0)
    test(1)
    test(2)
    testBig("-222222222222222222222222222222222222")
    testBig("-1")
    testBig("0")
    testBig("1")
    testBig("222222222222222222222222222222222222")
}

func test(n int) {
    fmt.Printf("Testing integer %3d:  ", n)
    
    if n&1 == 0 {
        fmt.Print("even ")
    } else {
        fmt.Print(" odd ")
    }
    
    
    
    if n%2 == 0 {
        fmt.Println("even")
    } else {
        fmt.Println(" odd")
    }
}

func testBig(s string) {
    b, _ := new(big.Int).SetString(s, 10)
    fmt.Printf("Testing big integer %v:  ", b)
    
    if b.Bit(0) == 0 {
        fmt.Println("even")
    } else {
        fmt.Println("odd")
    }
}






COBOL:        IF FUNCTION REM(Num, 2) = 0
           DISPLAY Num " is even."
       ELSE
           DISPLAY Num " is odd."
       END-IF
Task: Even or odd






PHP: 
echo (2 & 1) ? 'odd' : 'even';
echo (3 & 1) ? 'odd' : 'even';

echo (3 % 2) ? 'odd' : 'even';
echo (4 % 2) ? 'odd' : 'even';
Task: Even or odd






PHP: 
echo (2 & 1) ? 'odd' : 'even';
echo (3 & 1) ? 'odd' : 'even';

echo (3 % 2) ? 'odd' : 'even';
echo (4 % 2) ? 'odd' : 'even';






Ada: 



declare
   N : Integer := 5;
begin
   if N rem 2 = 0 then
      Put_Line ("Even number");
   elseif N rem 2 /= 0 then
      Put_Line ("Odd number");
   else
      Put_Line ("Something went really wrong!");
   end if;
end;
Task: Even or odd






PHP: 
echo (2 & 1) ? 'odd' : 'even';
echo (3 & 1) ? 'odd' : 'even';

echo (3 % 2) ? 'odd' : 'even';
echo (4 % 2) ? 'odd' : 'even';




COBOL:        IF FUNCTION REM(Num, 2) = 0
           DISPLAY Num " is even."
       ELSE
           DISPLAY Num " is odd."
       END-IF
Task: Even or odd


C++: bool isOdd(int x) 
{
    return x % 2;
}

bool isEven(int x)
{
    return !(x % 2);
}
Task: Quickselect algorithm

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

int qselect(int *v, int len, int k)
{
#	define SWAP(a, b) { tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
	int i, st, tmp;

	for (st = i = 0; i < len - 1; i++) {
		if (v[i] > v[len-1]) continue;
		SWAP(i, st);
		st++;
	}

	SWAP(len-1, st);

	return k == st	?v[st]
			:st > k	? qselect(v, st, k)
				: qselect(v + st, len - st, k - st);
}

int main(void)
{
#	define N (sizeof(x)/sizeof(x[0]))
	int x[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
	int y[N];

	int i;
	for (i = 0; i < 10; i++) {
		memcpy(y, x, sizeof(x)); 
		printf("%d: %d\n", i, qselect(y, 10, i));
	}

	return 0;
}
Task: Quickselect algorithm


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

int main() {
  for (int i = 0; i < 10; i++) {
    int a[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
    std::nth_element(a, a + i, a + sizeof(a)/sizeof(*a));
    std::cout << a[i];
    if (i < 9) std::cout << ", ";
  }
  std::cout << std::endl;

  return 0;
}
Task: Quickselect algorithm





Java: import java.util.Random;

public class QuickSelect {

	private static <E extends Comparable<? super E>> int partition(E[] arr, int left, int right, int pivot) {
		E pivotVal = arr[pivot];
		swap(arr, pivot, right);
		int storeIndex = left;
		for (int i = left; i < right; i++) {
			if (arr[i].compareTo(pivotVal) < 0) {
				swap(arr, i, storeIndex);
				storeIndex++;
			}
		}
		swap(arr, right, storeIndex);
		return storeIndex;
	}
	
	private static <E extends Comparable<? super E>> E select(E[] arr, int n) {
		int left = 0;
		int right = arr.length - 1;
		Random rand = new Random();
		while (right >= left) {
			int pivotIndex = partition(arr, left, right, rand.nextInt(right - left + 1) + left);
			if (pivotIndex == n) {
				return arr[pivotIndex];
			} else if (pivotIndex < n) {
				left = pivotIndex + 1;
			} else {
				right = pivotIndex - 1;
			}
		}
		return null;
	}
	
	private static void swap(Object[] arr, int i1, int i2) {
		if (i1 != i2) {
			Object temp = arr[i1];
			arr[i1] = arr[i2];
			arr[i2] = temp;
		}
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			Integer[] input = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
			System.out.print(select(input, i));
			if (i < 9) System.out.print(", ");
		}
		System.out.println();
	}

}
Task: Quickselect algorithm







Python: import random

def partition(vector, left, right, pivotIndex):
    pivotValue = vector[pivotIndex]
    vector[pivotIndex], vector[right] = vector[right], vector[pivotIndex]  
    storeIndex = left
    for i in range(left, right):
        if vector[i] < pivotValue:
            vector[storeIndex], vector[i] = vector[i], vector[storeIndex]
            storeIndex += 1
    vector[right], vector[storeIndex] = vector[storeIndex], vector[right]  
    return storeIndex

def _select(vector, left, right, k):
    "Returns the k-th smallest, (k >= 0), element of vector within vector[left:right+1] inclusive."
    while True:
        pivotIndex = random.randint(left, right)     
        pivotNewIndex = partition(vector, left, right, pivotIndex)
        pivotDist = pivotNewIndex - left
        if pivotDist == k:
            return vector[pivotNewIndex]
        elif k < pivotDist:
            right = pivotNewIndex - 1
        else:
            k -= pivotDist + 1
            left = pivotNewIndex + 1

def select(vector, k, left=None, right=None):
    
    if left is None:
        left = 0
    lv1 = len(vector) - 1
    if right is None:
        right = lv1
    assert vector and k >= 0, "Either null vector or k < 0 "
    assert 0 <= left <= lv1, "left is out of range"
    assert left <= right <= lv1, "right is out of range"
    return _select(vector, left, right, k)

if __name__ == '__main__':
    v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
    print([select(v, i) for i in range(10)])
Task: Quickselect algorithm








VB: Dim s As Variant
Private Function quick_select(ByRef s As Variant, k As Integer) As Integer
    Dim left As Integer, right As Integer, pos As Integer
    Dim pivotValue As Integer, tmp As Integer
    left = 1: right = UBound(s)
    Do While left < right
        pivotValue = s(k)
        tmp = s(k)
        s(k) = s(right)
        s(right) = tmp
        pos = left
        For i = left To right
            If s(i) < pivotValue Then
                tmp = s(i)
                s(i) = s(pos)
                s(pos) = tmp
                pos = pos + 1
            End If
        Next i
        tmp = s(right)
        s(right) = s(pos)
        s(pos) = tmp
        If pos = k Then
            Exit Do
        End If
        If pos < k Then
            left = pos + 1
        Else
            right = pos - 1
        End If
    Loop
    quick_select = s(k)
End Function
Public Sub main()
    Dim r As Integer, i As Integer
    s = [{9, 8, 7, 6, 5, 0, 1, 2, 3, 4}]
    For i = 1 To 10
        r = quick_select(s, i) 
        Debug.Print IIf(i < 10, r & ", ", "" & r);
    Next i
End Sub
Task: Quickselect algorithm




Go: package main

import "fmt"

func quickselect(list []int, k int) int {
    for {
        
        px := len(list) / 2
        pv := list[px]
        last := len(list) - 1
        list[px], list[last] = list[last], list[px]
        i := 0
        for j := 0; j < last; j++ {
            if list[j] < pv {
                list[i], list[j] = list[j], list[i]
                i++
            }
        }
        
        if i == k {
            return pv
        }
        if k < i {
            list = list[:i]
        } else {
            list[i], list[last] = list[last], list[i]
            list = list[i+1:]
            k -= i + 1
        }
    }
}

func main() {
    for i := 0; ; i++ {
        v := []int{9, 8, 7, 6, 5, 0, 1, 2, 3, 4}
        if i == len(v) {
            return
        }
        fmt.Println(quickselect(v, i))
    }
}
Task: Quickselect algorithm


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

int main() {
  for (int i = 0; i < 10; i++) {
    int a[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
    std::nth_element(a, a + i, a + sizeof(a)/sizeof(*a));
    std::cout << a[i];
    if (i < 9) std::cout << ", ";
  }
  std::cout << std::endl;

  return 0;
}









Fortran:       INTEGER FUNCTION FINDELEMENT(K,A,N)	
Chase an order statistic: FindElement(N/2,A,N) leads to the median, with some odd/even caution.
Careful
Charles Anthony Richard Hoare devised this method, as related to his famous QuickSort.
       INTEGER K,N		
       INTEGER A(N),HOPE,PESTY	
       INTEGER L,R,L2,R2	
        L = 1			
        R = N			
        DO WHILE (L .LT. R)	
          HOPE = A(K)		
          L2 = L		
          R2 = R		
          DO WHILE (L2 .LE. R2)	
            DO WHILE (A(L2) .LT. HOPE)	
              L2 = L2 + 1		
            END DO			
            DO WHILE (HOPE .LT. A(R2))	
              R2 = R2 - 1		
            END DO			
            IF (L2 - R2) 1,2,3	
    1       PESTY = A(L2)		
            A(L2) = A(R2)		
            A(R2) = PESTY		
    2       L2 = L2 + 1		
            R2 = R2 - 1		
    3     END DO		
          IF (R2 .LT. K) L = L2	
          IF (K .LT. L2) R = R2	
        END DO			
        FINDELEMENT = A(K)	
      END FUNCTION FINDELEMENT	

      PROGRAM POKE
      INTEGER FINDELEMENT	
      INTEGER N			
      PARAMETER (N = 10)	
      INTEGER A(66)		
      DATA A(1:N)/9, 8, 7, 6, 5, 0, 1, 2, 3, 4/	

      WRITE (6,1) A(1:N)	
    1 FORMAT ("Selection of the i'th element in order from an array.",/
     1 "The array need not be in order, and may be reordered.",/
     2 "  i Val:Array elements...",/,8X,666I2)

      DO I = 1,N	
        WRITE (6,2) I,FINDELEMENT(I,A,N),A(1:N)	
    2   FORMAT (I3,I4,":",666I2)	
      END DO		

      END
Task: Quickselect algorithm

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

int qselect(int *v, int len, int k)
{
#	define SWAP(a, b) { tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
	int i, st, tmp;

	for (st = i = 0; i < len - 1; i++) {
		if (v[i] > v[len-1]) continue;
		SWAP(i, st);
		st++;
	}

	SWAP(len-1, st);

	return k == st	?v[st]
			:st > k	? qselect(v, st, k)
				: qselect(v + st, len - st, k - st);
}

int main(void)
{
#	define N (sizeof(x)/sizeof(x[0]))
	int x[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
	int y[N];

	int i;
	for (i = 0; i < 10; i++) {
		memcpy(y, x, sizeof(x)); 
		printf("%d: %d\n", i, qselect(y, 10, i));
	}

	return 0;
}










Fortran:       INTEGER FUNCTION FINDELEMENT(K,A,N)	
Chase an order statistic: FindElement(N/2,A,N) leads to the median, with some odd/even caution.
Careful
Charles Anthony Richard Hoare devised this method, as related to his famous QuickSort.
       INTEGER K,N		
       INTEGER A(N),HOPE,PESTY	
       INTEGER L,R,L2,R2	
        L = 1			
        R = N			
        DO WHILE (L .LT. R)	
          HOPE = A(K)		
          L2 = L		
          R2 = R		
          DO WHILE (L2 .LE. R2)	
            DO WHILE (A(L2) .LT. HOPE)	
              L2 = L2 + 1		
            END DO			
            DO WHILE (HOPE .LT. A(R2))	
              R2 = R2 - 1		
            END DO			
            IF (L2 - R2) 1,2,3	
    1       PESTY = A(L2)		
            A(L2) = A(R2)		
            A(R2) = PESTY		
    2       L2 = L2 + 1		
            R2 = R2 - 1		
    3     END DO		
          IF (R2 .LT. K) L = L2	
          IF (K .LT. L2) R = R2	
        END DO			
        FINDELEMENT = A(K)	
      END FUNCTION FINDELEMENT	

      PROGRAM POKE
      INTEGER FINDELEMENT	
      INTEGER N			
      PARAMETER (N = 10)	
      INTEGER A(66)		
      DATA A(1:N)/9, 8, 7, 6, 5, 0, 1, 2, 3, 4/	

      WRITE (6,1) A(1:N)	
    1 FORMAT ("Selection of the i'th element in order from an array.",/
     1 "The array need not be in order, and may be reordered.",/
     2 "  i Val:Array elements...",/,8X,666I2)

      DO I = 1,N	
        WRITE (6,2) I,FINDELEMENT(I,A,N),A(1:N)	
    2   FORMAT (I3,I4,":",666I2)	
      END DO		

      END
Task: Quickselect algorithm





Java: import java.util.Random;

public class QuickSelect {

	private static <E extends Comparable<? super E>> int partition(E[] arr, int left, int right, int pivot) {
		E pivotVal = arr[pivot];
		swap(arr, pivot, right);
		int storeIndex = left;
		for (int i = left; i < right; i++) {
			if (arr[i].compareTo(pivotVal) < 0) {
				swap(arr, i, storeIndex);
				storeIndex++;
			}
		}
		swap(arr, right, storeIndex);
		return storeIndex;
	}
	
	private static <E extends Comparable<? super E>> E select(E[] arr, int n) {
		int left = 0;
		int right = arr.length - 1;
		Random rand = new Random();
		while (right >= left) {
			int pivotIndex = partition(arr, left, right, rand.nextInt(right - left + 1) + left);
			if (pivotIndex == n) {
				return arr[pivotIndex];
			} else if (pivotIndex < n) {
				left = pivotIndex + 1;
			} else {
				right = pivotIndex - 1;
			}
		}
		return null;
	}
	
	private static void swap(Object[] arr, int i1, int i2) {
		if (i1 != i2) {
			Object temp = arr[i1];
			arr[i1] = arr[i2];
			arr[i2] = temp;
		}
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			Integer[] input = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
			System.out.print(select(input, i));
			if (i < 9) System.out.print(", ");
		}
		System.out.println();
	}

}






Fortran:       INTEGER FUNCTION FINDELEMENT(K,A,N)	
Chase an order statistic: FindElement(N/2,A,N) leads to the median, with some odd/even caution.
Careful
Charles Anthony Richard Hoare devised this method, as related to his famous QuickSort.
       INTEGER K,N		
       INTEGER A(N),HOPE,PESTY	
       INTEGER L,R,L2,R2	
        L = 1			
        R = N			
        DO WHILE (L .LT. R)	
          HOPE = A(K)		
          L2 = L		
          R2 = R		
          DO WHILE (L2 .LE. R2)	
            DO WHILE (A(L2) .LT. HOPE)	
              L2 = L2 + 1		
            END DO			
            DO WHILE (HOPE .LT. A(R2))	
              R2 = R2 - 1		
            END DO			
            IF (L2 - R2) 1,2,3	
    1       PESTY = A(L2)		
            A(L2) = A(R2)		
            A(R2) = PESTY		
    2       L2 = L2 + 1		
            R2 = R2 - 1		
    3     END DO		
          IF (R2 .LT. K) L = L2	
          IF (K .LT. L2) R = R2	
        END DO			
        FINDELEMENT = A(K)	
      END FUNCTION FINDELEMENT	

      PROGRAM POKE
      INTEGER FINDELEMENT	
      INTEGER N			
      PARAMETER (N = 10)	
      INTEGER A(66)		
      DATA A(1:N)/9, 8, 7, 6, 5, 0, 1, 2, 3, 4/	

      WRITE (6,1) A(1:N)	
    1 FORMAT ("Selection of the i'th element in order from an array.",/
     1 "The array need not be in order, and may be reordered.",/
     2 "  i Val:Array elements...",/,8X,666I2)

      DO I = 1,N	
        WRITE (6,2) I,FINDELEMENT(I,A,N),A(1:N)	
    2   FORMAT (I3,I4,":",666I2)	
      END DO		

      END
Task: Quickselect algorithm







Python: import random

def partition(vector, left, right, pivotIndex):
    pivotValue = vector[pivotIndex]
    vector[pivotIndex], vector[right] = vector[right], vector[pivotIndex]  
    storeIndex = left
    for i in range(left, right):
        if vector[i] < pivotValue:
            vector[storeIndex], vector[i] = vector[i], vector[storeIndex]
            storeIndex += 1
    vector[right], vector[storeIndex] = vector[storeIndex], vector[right]  
    return storeIndex

def _select(vector, left, right, k):
    "Returns the k-th smallest, (k >= 0), element of vector within vector[left:right+1] inclusive."
    while True:
        pivotIndex = random.randint(left, right)     
        pivotNewIndex = partition(vector, left, right, pivotIndex)
        pivotDist = pivotNewIndex - left
        if pivotDist == k:
            return vector[pivotNewIndex]
        elif k < pivotDist:
            right = pivotNewIndex - 1
        else:
            k -= pivotDist + 1
            left = pivotNewIndex + 1

def select(vector, k, left=None, right=None):
    
    if left is None:
        left = 0
    lv1 = len(vector) - 1
    if right is None:
        right = lv1
    assert vector and k >= 0, "Either null vector or k < 0 "
    assert 0 <= left <= lv1, "left is out of range"
    assert left <= right <= lv1, "right is out of range"
    return _select(vector, left, right, k)

if __name__ == '__main__':
    v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
    print([select(v, i) for i in range(10)])




Fortran:       INTEGER FUNCTION FINDELEMENT(K,A,N)	
Chase an order statistic: FindElement(N/2,A,N) leads to the median, with some odd/even caution.
Careful
Charles Anthony Richard Hoare devised this method, as related to his famous QuickSort.
       INTEGER K,N		
       INTEGER A(N),HOPE,PESTY	
       INTEGER L,R,L2,R2	
        L = 1			
        R = N			
        DO WHILE (L .LT. R)	
          HOPE = A(K)		
          L2 = L		
          R2 = R		
          DO WHILE (L2 .LE. R2)	
            DO WHILE (A(L2) .LT. HOPE)	
              L2 = L2 + 1		
            END DO			
            DO WHILE (HOPE .LT. A(R2))	
              R2 = R2 - 1		
            END DO			
            IF (L2 - R2) 1,2,3	
    1       PESTY = A(L2)		
            A(L2) = A(R2)		
            A(R2) = PESTY		
    2       L2 = L2 + 1		
            R2 = R2 - 1		
    3     END DO		
          IF (R2 .LT. K) L = L2	
          IF (K .LT. L2) R = R2	
        END DO			
        FINDELEMENT = A(K)	
      END FUNCTION FINDELEMENT	

      PROGRAM POKE
      INTEGER FINDELEMENT	
      INTEGER N			
      PARAMETER (N = 10)	
      INTEGER A(66)		
      DATA A(1:N)/9, 8, 7, 6, 5, 0, 1, 2, 3, 4/	

      WRITE (6,1) A(1:N)	
    1 FORMAT ("Selection of the i'th element in order from an array.",/
     1 "The array need not be in order, and may be reordered.",/
     2 "  i Val:Array elements...",/,8X,666I2)

      DO I = 1,N	
        WRITE (6,2) I,FINDELEMENT(I,A,N),A(1:N)	
    2   FORMAT (I3,I4,":",666I2)	
      END DO		

      END
Task: Quickselect algorithm








VB: Dim s As Variant
Private Function quick_select(ByRef s As Variant, k As Integer) As Integer
    Dim left As Integer, right As Integer, pos As Integer
    Dim pivotValue As Integer, tmp As Integer
    left = 1: right = UBound(s)
    Do While left < right
        pivotValue = s(k)
        tmp = s(k)
        s(k) = s(right)
        s(right) = tmp
        pos = left
        For i = left To right
            If s(i) < pivotValue Then
                tmp = s(i)
                s(i) = s(pos)
                s(pos) = tmp
                pos = pos + 1
            End If
        Next i
        tmp = s(right)
        s(right) = s(pos)
        s(pos) = tmp
        If pos = k Then
            Exit Do
        End If
        If pos < k Then
            left = pos + 1
        Else
            right = pos - 1
        End If
    Loop
    quick_select = s(k)
End Function
Public Sub main()
    Dim r As Integer, i As Integer
    s = [{9, 8, 7, 6, 5, 0, 1, 2, 3, 4}]
    For i = 1 To 10
        r = quick_select(s, i) 
        Debug.Print IIf(i < 10, r & ", ", "" & r);
    Next i
End Sub



Fortran:       INTEGER FUNCTION FINDELEMENT(K,A,N)	
Chase an order statistic: FindElement(N/2,A,N) leads to the median, with some odd/even caution.
Careful
Charles Anthony Richard Hoare devised this method, as related to his famous QuickSort.
       INTEGER K,N		
       INTEGER A(N),HOPE,PESTY	
       INTEGER L,R,L2,R2	
        L = 1			
        R = N			
        DO WHILE (L .LT. R)	
          HOPE = A(K)		
          L2 = L		
          R2 = R		
          DO WHILE (L2 .LE. R2)	
            DO WHILE (A(L2) .LT. HOPE)	
              L2 = L2 + 1		
            END DO			
            DO WHILE (HOPE .LT. A(R2))	
              R2 = R2 - 1		
            END DO			
            IF (L2 - R2) 1,2,3	
    1       PESTY = A(L2)		
            A(L2) = A(R2)		
            A(R2) = PESTY		
    2       L2 = L2 + 1		
            R2 = R2 - 1		
    3     END DO		
          IF (R2 .LT. K) L = L2	
          IF (K .LT. L2) R = R2	
        END DO			
        FINDELEMENT = A(K)	
      END FUNCTION FINDELEMENT	

      PROGRAM POKE
      INTEGER FINDELEMENT	
      INTEGER N			
      PARAMETER (N = 10)	
      INTEGER A(66)		
      DATA A(1:N)/9, 8, 7, 6, 5, 0, 1, 2, 3, 4/	

      WRITE (6,1) A(1:N)	
    1 FORMAT ("Selection of the i'th element in order from an array.",/
     1 "The array need not be in order, and may be reordered.",/
     2 "  i Val:Array elements...",/,8X,666I2)

      DO I = 1,N	
        WRITE (6,2) I,FINDELEMENT(I,A,N),A(1:N)	
    2   FORMAT (I3,I4,":",666I2)	
      END DO		

      END
Task: Kaprekar numbers

C: #include <stdio.h>
#include <stdint.h>
typedef uint64_t ulong;

int kaprekar(ulong n, int base)
{
	ulong nn = n * n, r, tens = 1;

	if ((nn - n) % (base - 1)) return 0;

	while (tens < n) tens *= base;
	if (n == tens) return 1 == n;

	while ((r = nn % tens) < n) {
		if (nn / tens + r == n) return tens;
		tens *= base;
	}

	return 0;
}

void print_num(ulong n, int base)
{
	ulong q, div = base;

	while (div < n) div *= base;
	while (n && (div /= base)) {
		q = n / div;
		if (q < 10)     putchar(q + '0');
		else            putchar(q + 'a' - 10);
		n -= q * div;
	}
}

int main()
{
	ulong i, tens;
	int cnt = 0;
	int base = 10;

	printf("base 10:\n");
	for (i = 1; i < 1000000; i++)
		if (kaprekar(i, base))
			printf("%3d: %llu\n", ++cnt, i);

	base = 17;
	printf("\nbase %d:\n  1: 1\n", base);
	for (i = 2, cnt = 1; i < 1000000; i++)
		if ((tens = kaprekar(i, base))) {
			printf("%3d: %llu", ++cnt, i);
			printf(" \t"); print_num(i, base);
			printf("\t");  print_num(i * i, base);
			printf("\t");  print_num(i * i / tens, base);
			printf(" + "); print_num(i * i % tens, base);
			printf("\n");
		}

	return 0;
}
Task: Kaprekar numbers



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

public class KaprekarNumbers {

    
    
    
    public static void Main() {
        int count = 0;

        foreach ( ulong i in _kaprekarGenerator(999999) ) {
            Console.WriteLine(i);
            count++;
        }

        Console.WriteLine("There are {0} Kaprekar numbers less than 1000000.", count);
    }

    
    
    
    
    
    private static IEnumerable<ulong> _kaprekarGenerator(ulong max) {

        ulong next = 1;

        
        yield return next;

        for ( next = 2; next <= max; next++ ) {

            ulong square = next * next;

            for ( ulong check = 10; check <= 10000000000000000000; check *= 10 ) {
                
                

                
                if ( square <= check )
                    break;

                
                
                
                
                

                ulong r = square % check;
                ulong q = (square - r) / check;

                if ( r != 0 && q + r == next ) {
                    yield return next;
                    break;
                }
            }

        }

    }

}