kazuyamaa/multi-language-messages-01 huggingface dataset:
Task: Vector products
C++: #include <iostream>

template< class T >
class D3Vector {

template< class U >
friend std::ostream & operator<<( std::ostream & , const D3Vector<U> & ) ;   

public :
   D3Vector( T a , T b , T c ) {
      x = a ;
      y = b ;
      z = c ;
   }

   T dotproduct ( const D3Vector & rhs ) {
      T scalar = x * rhs.x + y * rhs.y + z * rhs.z ;
      return scalar ;
   }

   D3Vector crossproduct ( const D3Vector & rhs ) {
      T a = y * rhs.z - z * rhs.y ;
      T b = z * rhs.x - x * rhs.z ;
      T c = x * rhs.y - y * rhs.x ;
      D3Vector product( a , b , c ) ;
      return product ;
   }

   D3Vector triplevec( D3Vector & a , D3Vector & b ) {
      return crossproduct ( a.crossproduct( b ) ) ;
   }

   T triplescal( D3Vector & a, D3Vector & b ) {
      return dotproduct( a.crossproduct( b ) ) ;
   }

private :
   T x , y , z ;  
} ;

template< class T >
std::ostream & operator<< ( std::ostream & os , const D3Vector<T> & vec ) {
   os << "( "  << vec.x << " ,  " << vec.y << " ,  " << vec.z << " )" ;
   return os ;
}

int main( ) {
   D3Vector<int> a( 3 , 4 , 5 ) , b ( 4 , 3 , 5 ) , c( -5 , -12 , -13 ) ;
   std::cout << "a . b : " << a.dotproduct( b ) << "\n" ;
   std::cout << "a x b : " << a.crossproduct( b ) << "\n" ;
   std::cout << "a . b x c : " << a.triplescal( b , c ) << "\n" ;
   std::cout << "a x b x c : " << a.triplevec( b , c ) << "\n" ;
   return 0 ;
}

VB: Option Base 1
Function dot_product(a As Variant, b As Variant) As Variant
    dot_product = WorksheetFunction.SumProduct(a, b)
End Function
 
Function cross_product(a As Variant, b As Variant) As Variant
    cross_product = Array(a(2) * b(3) - a(3) * b(2), a(3) * b(1) - a(1) * b(3), a(1) * b(2) - a(2) * b(1))
End Function
 
Function scalar_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    scalar_triple_product = dot_product(a, cross_product(b, c))
End Function
 
Function vector_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    vector_triple_product = cross_product(a, cross_product(b, c))
End Function
 
Public Sub main()
    a = [{3, 4, 5}]
    b = [{4, 3, 5}]
    c = [{-5, -12, -13}]
    Debug.Print "      a . b = "; dot_product(a, b)
    Debug.Print "      a x b = "; "("; Join(cross_product(a, b), ", "); ")"
    Debug.Print "a . (b x c) = "; scalar_triple_product(a, b, c)
    Debug.Print "a x (b x c) = "; "("; Join(vector_triple_product(a, b, c), ", "); ")"
End Sub
Task: Vector products
C#: using System;
using System.Windows.Media.Media3D;

class VectorProducts
{
    static double ScalarTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.DotProduct(a, Vector3D.CrossProduct(b, c));
    }

    static Vector3D VectorTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.CrossProduct(a, Vector3D.CrossProduct(b, c));
    }

    static void Main()
    {
        var a = new Vector3D(3, 4, 5);
        var b = new Vector3D(4, 3, 5);
        var c = new Vector3D(-5, -12, -13);

        Console.WriteLine(Vector3D.DotProduct(a, b));
        Console.WriteLine(Vector3D.CrossProduct(a, b));
        Console.WriteLine(ScalarTripleProduct(a, b, c));
        Console.WriteLine(VectorTripleProduct(a, b, c));
    }
}

Go: package main

import "fmt"

type vector struct {
    x, y, z float64
}

var (
    a = vector{3, 4, 5}
    b = vector{4, 3, 5}
    c = vector{-5, -12, -13}
)

func dot(a, b vector) float64 {
    return a.x*b.x + a.y*b.y + a.z*b.z
}

func cross(a, b vector) vector {
    return vector{a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}
}

func s3(a, b, c vector) float64 {
    return dot(a, cross(b, c))
}

func v3(a, b, c vector) vector {
    return cross(a, cross(b, c))
}

func main() {
    fmt.Println(dot(a, b))
    fmt.Println(cross(a, b))
    fmt.Println(s3(a, b, c))
    fmt.Println(v3(a, b, c))
}
Task: Vector products
C#: using System;
using System.Windows.Media.Media3D;

class VectorProducts
{
    static double ScalarTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.DotProduct(a, Vector3D.CrossProduct(b, c));
    }

    static Vector3D VectorTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.CrossProduct(a, Vector3D.CrossProduct(b, c));
    }

    static void Main()
    {
        var a = new Vector3D(3, 4, 5);
        var b = new Vector3D(4, 3, 5);
        var c = new Vector3D(-5, -12, -13);

        Console.WriteLine(Vector3D.DotProduct(a, b));
        Console.WriteLine(Vector3D.CrossProduct(a, b));
        Console.WriteLine(ScalarTripleProduct(a, b, c));
        Console.WriteLine(VectorTripleProduct(a, b, c));
    }
}

Python: def crossp(a, b):
    
    assert len(a) == len(b) == 3, 'For 3D vectors only'
    a1, a2, a3 = a
    b1, b2, b3 = b
    return (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1)
 
def dotp(a,b):
    
    assert len(a) == len(b), 'Vector sizes must match'
    return sum(aterm * bterm for aterm,bterm in zip(a, b))
 
def scalartriplep(a, b, c):
    
    return dotp(a, crossp(b, c))
 
def vectortriplep(a, b, c):
    
    return crossp(a, crossp(b, c))
 
if __name__ == '__main__':
    a, b, c = (3, 4, 5), (4, 3, 5), (-5, -12, -13)
    print("a = %r;  b = %r;  c = %r" % (a, b, c))
    print("a . b = %r" % dotp(a,b))
    print("a x b = %r"  % (crossp(a,b),))
    print("a . (b x c) = %r" % scalartriplep(a, b, c))
    print("a x (b x c) = %r" % (vectortriplep(a, b, c),))
Task: Vector products
C#: using System;
using System.Windows.Media.Media3D;

class VectorProducts
{
    static double ScalarTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.DotProduct(a, Vector3D.CrossProduct(b, c));
    }

    static Vector3D VectorTripleProduct(Vector3D a, Vector3D b, Vector3D c)
    {
        return Vector3D.CrossProduct(a, Vector3D.CrossProduct(b, c));
    }

    static void Main()
    {
        var a = new Vector3D(3, 4, 5);
        var b = new Vector3D(4, 3, 5);
        var c = new Vector3D(-5, -12, -13);

        Console.WriteLine(Vector3D.DotProduct(a, b));
        Console.WriteLine(Vector3D.CrossProduct(a, b));
        Console.WriteLine(ScalarTripleProduct(a, b, c));
        Console.WriteLine(VectorTripleProduct(a, b, c));
    }
}

VB: Option Base 1
Function dot_product(a As Variant, b As Variant) As Variant
    dot_product = WorksheetFunction.SumProduct(a, b)
End Function
 
Function cross_product(a As Variant, b As Variant) As Variant
    cross_product = Array(a(2) * b(3) - a(3) * b(2), a(3) * b(1) - a(1) * b(3), a(1) * b(2) - a(2) * b(1))
End Function
 
Function scalar_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    scalar_triple_product = dot_product(a, cross_product(b, c))
End Function
 
Function vector_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    vector_triple_product = cross_product(a, cross_product(b, c))
End Function
 
Public Sub main()
    a = [{3, 4, 5}]
    b = [{4, 3, 5}]
    c = [{-5, -12, -13}]
    Debug.Print "      a . b = "; dot_product(a, b)
    Debug.Print "      a x b = "; "("; Join(cross_product(a, b), ", "); ")"
    Debug.Print "a . (b x c) = "; scalar_triple_product(a, b, c)
    Debug.Print "a x (b x c) = "; "("; Join(vector_triple_product(a, b, c), ", "); ")"
End Sub
Task: Vector products
Go: package main

import "fmt"

type vector struct {
    x, y, z float64
}

var (
    a = vector{3, 4, 5}
    b = vector{4, 3, 5}
    c = vector{-5, -12, -13}
)

func dot(a, b vector) float64 {
    return a.x*b.x + a.y*b.y + a.z*b.z
}

func cross(a, b vector) vector {
    return vector{a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}
}

func s3(a, b, c vector) float64 {
    return dot(a, cross(b, c))
}

func v3(a, b, c vector) vector {
    return cross(a, cross(b, c))
}

func main() {
    fmt.Println(dot(a, b))
    fmt.Println(cross(a, b))
    fmt.Println(s3(a, b, c))
    fmt.Println(v3(a, b, c))
}

Python: def crossp(a, b):
    
    assert len(a) == len(b) == 3, 'For 3D vectors only'
    a1, a2, a3 = a
    b1, b2, b3 = b
    return (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1)
 
def dotp(a,b):
    
    assert len(a) == len(b), 'Vector sizes must match'
    return sum(aterm * bterm for aterm,bterm in zip(a, b))
 
def scalartriplep(a, b, c):
    
    return dotp(a, crossp(b, c))
 
def vectortriplep(a, b, c):
    
    return crossp(a, crossp(b, c))
 
if __name__ == '__main__':
    a, b, c = (3, 4, 5), (4, 3, 5), (-5, -12, -13)
    print("a = %r;  b = %r;  c = %r" % (a, b, c))
    print("a . b = %r" % dotp(a,b))
    print("a x b = %r"  % (crossp(a,b),))
    print("a . (b x c) = %r" % scalartriplep(a, b, c))
    print("a x (b x c) = %r" % (vectortriplep(a, b, c),))
Task: Vector products
Go: package main

import "fmt"

type vector struct {
    x, y, z float64
}

var (
    a = vector{3, 4, 5}
    b = vector{4, 3, 5}
    c = vector{-5, -12, -13}
)

func dot(a, b vector) float64 {
    return a.x*b.x + a.y*b.y + a.z*b.z
}

func cross(a, b vector) vector {
    return vector{a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}
}

func s3(a, b, c vector) float64 {
    return dot(a, cross(b, c))
}

func v3(a, b, c vector) vector {
    return cross(a, cross(b, c))
}

func main() {
    fmt.Println(dot(a, b))
    fmt.Println(cross(a, b))
    fmt.Println(s3(a, b, c))
    fmt.Println(v3(a, b, c))
}

VB: Option Base 1
Function dot_product(a As Variant, b As Variant) As Variant
    dot_product = WorksheetFunction.SumProduct(a, b)
End Function
 
Function cross_product(a As Variant, b As Variant) As Variant
    cross_product = Array(a(2) * b(3) - a(3) * b(2), a(3) * b(1) - a(1) * b(3), a(1) * b(2) - a(2) * b(1))
End Function
 
Function scalar_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    scalar_triple_product = dot_product(a, cross_product(b, c))
End Function
 
Function vector_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    vector_triple_product = cross_product(a, cross_product(b, c))
End Function
 
Public Sub main()
    a = [{3, 4, 5}]
    b = [{4, 3, 5}]
    c = [{-5, -12, -13}]
    Debug.Print "      a . b = "; dot_product(a, b)
    Debug.Print "      a x b = "; "("; Join(cross_product(a, b), ", "); ")"
    Debug.Print "a . (b x c) = "; scalar_triple_product(a, b, c)
    Debug.Print "a x (b x c) = "; "("; Join(vector_triple_product(a, b, c), ", "); ")"
End Sub
Task: Vector products
Python: def crossp(a, b):
    
    assert len(a) == len(b) == 3, 'For 3D vectors only'
    a1, a2, a3 = a
    b1, b2, b3 = b
    return (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1)
 
def dotp(a,b):
    
    assert len(a) == len(b), 'Vector sizes must match'
    return sum(aterm * bterm for aterm,bterm in zip(a, b))
 
def scalartriplep(a, b, c):
    
    return dotp(a, crossp(b, c))
 
def vectortriplep(a, b, c):
    
    return crossp(a, crossp(b, c))
 
if __name__ == '__main__':
    a, b, c = (3, 4, 5), (4, 3, 5), (-5, -12, -13)
    print("a = %r;  b = %r;  c = %r" % (a, b, c))
    print("a . b = %r" % dotp(a,b))
    print("a x b = %r"  % (crossp(a,b),))
    print("a . (b x c) = %r" % scalartriplep(a, b, c))
    print("a x (b x c) = %r" % (vectortriplep(a, b, c),))

VB: Option Base 1
Function dot_product(a As Variant, b As Variant) As Variant
    dot_product = WorksheetFunction.SumProduct(a, b)
End Function
 
Function cross_product(a As Variant, b As Variant) As Variant
    cross_product = Array(a(2) * b(3) - a(3) * b(2), a(3) * b(1) - a(1) * b(3), a(1) * b(2) - a(2) * b(1))
End Function
 
Function scalar_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    scalar_triple_product = dot_product(a, cross_product(b, c))
End Function
 
Function vector_triple_product(a As Variant, b As Variant, c As Variant) As Variant
    vector_triple_product = cross_product(a, cross_product(b, c))
End Function
 
Public Sub main()
    a = [{3, 4, 5}]
    b = [{4, 3, 5}]
    c = [{-5, -12, -13}]
    Debug.Print "      a . b = "; dot_product(a, b)
    Debug.Print "      a x b = "; "("; Join(cross_product(a, b), ", "); ")"
    Debug.Print "a . (b x c) = "; scalar_triple_product(a, b, c)
    Debug.Print "a x (b x c) = "; "("; Join(vector_triple_product(a, b, c), ", "); ")"
End Sub
Task: Vector products
C++: #include <iostream>

template< class T >
class D3Vector {

template< class U >
friend std::ostream & operator<<( std::ostream & , const D3Vector<U> & ) ;   

public :
   D3Vector( T a , T b , T c ) {
      x = a ;
      y = b ;
      z = c ;
   }

   T dotproduct ( const D3Vector & rhs ) {
      T scalar = x * rhs.x + y * rhs.y + z * rhs.z ;
      return scalar ;
   }

   D3Vector crossproduct ( const D3Vector & rhs ) {
      T a = y * rhs.z - z * rhs.y ;
      T b = z * rhs.x - x * rhs.z ;
      T c = x * rhs.y - y * rhs.x ;
      D3Vector product( a , b , c ) ;
      return product ;
   }

   D3Vector triplevec( D3Vector & a , D3Vector & b ) {
      return crossproduct ( a.crossproduct( b ) ) ;
   }

   T triplescal( D3Vector & a, D3Vector & b ) {
      return dotproduct( a.crossproduct( b ) ) ;
   }

private :
   T x , y , z ;  
} ;

template< class T >
std::ostream & operator<< ( std::ostream & os , const D3Vector<T> & vec ) {
   os << "( "  << vec.x << " ,  " << vec.y << " ,  " << vec.z << " )" ;
   return os ;
}

int main( ) {
   D3Vector<int> a( 3 , 4 , 5 ) , b ( 4 , 3 , 5 ) , c( -5 , -12 , -13 ) ;
   std::cout << "a . b : " << a.dotproduct( b ) << "\n" ;
   std::cout << "a x b : " << a.crossproduct( b ) << "\n" ;
   std::cout << "a . b x c : " << a.triplescal( b , c ) << "\n" ;
   std::cout << "a x b x c : " << a.triplevec( b , c ) << "\n" ;
   return 0 ;
}

Python: def crossp(a, b):
    
    assert len(a) == len(b) == 3, 'For 3D vectors only'
    a1, a2, a3 = a
    b1, b2, b3 = b
    return (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1)
 
def dotp(a,b):
    
    assert len(a) == len(b), 'Vector sizes must match'
    return sum(aterm * bterm for aterm,bterm in zip(a, b))
 
def scalartriplep(a, b, c):
    
    return dotp(a, crossp(b, c))
 
def vectortriplep(a, b, c):
    
    return crossp(a, crossp(b, c))
 
if __name__ == '__main__':
    a, b, c = (3, 4, 5), (4, 3, 5), (-5, -12, -13)
    print("a = %r;  b = %r;  c = %r" % (a, b, c))
    print("a . b = %r" % dotp(a,b))
    print("a x b = %r"  % (crossp(a,b),))
    print("a . (b x c) = %r" % scalartriplep(a, b, c))
    print("a x (b x c) = %r" % (vectortriplep(a, b, c),))
Task: Cycle detection
C: #include <stdio.h>
#include <stdlib.h>

typedef int(*I2I)(int);
typedef struct {
    int a, b;
} Pair;

Pair brent(I2I f, int x0) {
    int power = 1, lam = 1, tortoise = x0, hare, mu, i;
    Pair result;

    hare = (*f)(x0);
    while (tortoise != hare) {
        if (power == lam) {
            tortoise = hare;
            power = power * 2;
            lam = 0;
        }
        hare = (*f)(hare);
        lam++;
    }

    hare = x0;
    i = 0;
    while (i < lam) {
        hare = (*f)(hare);
        i++;
    }

    tortoise = x0;
    mu = 0;
    while (tortoise != hare) {
        tortoise = (*f)(tortoise);
        hare = (*f)(hare);
        mu++;
    }

    result.a = lam;
    result.b = mu;
    return result;
}

int lambda(int x) {
    return (x*x + 1) % 255;
}

int main() {
    int x0 = 3, x = 3, i;
    Pair result;

    printf("[3");
    for (i = 1; i <= 40; ++i) {
        x = lambda(x);
        printf(", %d", x);
    }
    printf("]\n");

    result = brent(lambda, x0);
    printf("Cycle length = %d\nStart index = %d\nCycle = [", result.a, result.b);

    x0 = 3;
    x = x0;
    for (i = 1; i <= result.b; ++i) {
        x = lambda(x);
    }
    for (i = 1; i <= result.a; ++i) {
        if (i > 1) {
            printf(", ");
        }

        printf("%d", x);
        x = lambda(x);
    }

    printf("]\n");

    return 0;
}

C#: struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
 };
 
ListNode* Solution::detectCycle(ListNode* A) {
    ListNode* slow = A;
    ListNode* fast = A;
    ListNode* cycleNode = 0;
    while (slow && fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
        {
            cycleNode = slow;
            break;
        }
    }
    if (cycleNode == 0)
    {
        return 0;
    }
    std::set<ListNode*> setPerimeter;
    setPerimeter.insert(cycleNode);
    for (ListNode* pNode = cycleNode->next; pNode != cycleNode; pNode = pNode->next)
    setPerimeter.insert(pNode);
    for (ListNode* pNode = A; true; pNode = pNode->next)
    {
        std::set<ListNode*>::iterator iter = setPerimeter.find(pNode);
        if (iter != setPerimeter.end()) 
        {
            return pNode;
        }
    }
}
Task: Cycle detection
C: #include <stdio.h>
#include <stdlib.h>

typedef int(*I2I)(int);
typedef struct {
    int a, b;
} Pair;

Pair brent(I2I f, int x0) {
    int power = 1, lam = 1, tortoise = x0, hare, mu, i;
    Pair result;

    hare = (*f)(x0);
    while (tortoise != hare) {
        if (power == lam) {
            tortoise = hare;
            power = power * 2;
            lam = 0;
        }
        hare = (*f)(hare);
        lam++;
    }

    hare = x0;
    i = 0;
    while (i < lam) {
        hare = (*f)(hare);
        i++;
    }

    tortoise = x0;
    mu = 0;
    while (tortoise != hare) {
        tortoise = (*f)(tortoise);
        hare = (*f)(hare);
        mu++;
    }

    result.a = lam;
    result.b = mu;
    return result;
}

int lambda(int x) {
    return (x*x + 1) % 255;
}

int main() {
    int x0 = 3, x = 3, i;
    Pair result;

    printf("[3");
    for (i = 1; i <= 40; ++i) {
        x = lambda(x);
        printf(", %d", x);
    }
    printf("]\n");

    result = brent(lambda, x0);
    printf("Cycle length = %d\nStart index = %d\nCycle = [", result.a, result.b);

    x0 = 3;
    x = x0;
    for (i = 1; i <= result.b; ++i) {
        x = lambda(x);
    }
    for (i = 1; i <= result.a; ++i) {
        if (i > 1) {
            printf(", ");
        }

        printf("%d", x);
        x = lambda(x);
    }

    printf("]\n");

    return 0;
}

Java: import java.util.function.*;
import static java.util.stream.IntStream.*;

public class CycleDetection {

    public static void main(String[] args) {
        brent(i -> (i * i + 1) % 255, 3);
    }

    static void brent(IntUnaryOperator f, int x0) {
        int cycleLength;
        int hare = x0;
        FOUND:
        for (int power = 1; ; power *= 2) {
            int tortoise = hare;
            for (int i = 1; i <= power; i++) {
                hare = f.applyAsInt(hare);
                 if (tortoise == hare) {
                    cycleLength = i;
                    break FOUND;
                }
            }
        }

        hare = x0;
        for (int i = 0; i < cycleLength; i++)
            hare = f.applyAsInt(hare);

        int cycleStart = 0;
        for (int tortoise = x0; tortoise != hare; cycleStart++) {
            tortoise = f.applyAsInt(tortoise);
            hare = f.applyAsInt(hare);
        }

        printResult(x0, f, cycleLength, cycleStart);
    }

    static void printResult(int x0, IntUnaryOperator f, int len, int start) {
        System.out.printf("Cycle length: %d%nCycle: ", len);
        iterate(x0, f).skip(start).limit(len)
                .forEach(n -> System.out.printf("%s ", n));
    }
}
Task: Cycle detection
C: #include <stdio.h>
#include <stdlib.h>

typedef int(*I2I)(int);
typedef struct {
    int a, b;
} Pair;

Pair brent(I2I f, int x0) {
    int power = 1, lam = 1, tortoise = x0, hare, mu, i;
    Pair result;

    hare = (*f)(x0);
    while (tortoise != hare) {
        if (power == lam) {
            tortoise = hare;
            power = power * 2;
            lam = 0;
        }
        hare = (*f)(hare);
        lam++;
    }

    hare = x0;
    i = 0;
    while (i < lam) {
        hare = (*f)(hare);
        i++;
    }

    tortoise = x0;
    mu = 0;
    while (tortoise != hare) {
        tortoise = (*f)(tortoise);
        hare = (*f)(hare);
        mu++;
    }

    result.a = lam;
    result.b = mu;
    return result;
}

int lambda(int x) {
    return (x*x + 1) % 255;
}

int main() {
    int x0 = 3, x = 3, i;
    Pair result;

    printf("[3");
    for (i = 1; i <= 40; ++i) {
        x = lambda(x);
        printf(", %d", x);
    }
    printf("]\n");

    result = brent(lambda, x0);
    printf("Cycle length = %d\nStart index = %d\nCycle = [", result.a, result.b);

    x0 = 3;
    x = x0;
    for (i = 1; i <= result.b; ++i) {
        x = lambda(x);
    }
    for (i = 1; i <= result.a; ++i) {
        if (i > 1) {
            printf(", ");
        }

        printf("%d", x);
        x = lambda(x);
    }

    printf("]\n");

    return 0;
}

Python: import itertools

def brent(f, x0):
    
    power = lam = 1
    tortoise = x0
    hare = f(x0)  
    while tortoise != hare:
        if power == lam:  
            tortoise = hare
            power *= 2
            lam = 0
        hare = f(hare)
        lam += 1

    
    mu = 0
    tortoise = hare = x0
    for i in range(lam):
    
        hare = f(hare)
    

    
    while tortoise != hare:
        tortoise = f(tortoise)
        hare = f(hare)
        mu += 1
 
    return lam, mu

def iterate(f, x0):
    while True:
        yield x0
        x0 = f(x0)

if __name__ == '__main__':
    f = lambda x: (x * x + 1) % 255
    x0 = 3
    lam, mu = brent(f, x0)
    print("Cycle length: %d" % lam)
    print("Cycle start index: %d" % mu)
    print("Cycle: %s" % list(itertools.islice(iterate(f, x0), mu, mu+lam)))
Task: Cycle detection
C: #include <stdio.h>
#include <stdlib.h>

typedef int(*I2I)(int);
typedef struct {
    int a, b;
} Pair;

Pair brent(I2I f, int x0) {
    int power = 1, lam = 1, tortoise = x0, hare, mu, i;
    Pair result;

    hare = (*f)(x0);
    while (tortoise != hare) {
        if (power == lam) {
            tortoise = hare;
            power = power * 2;
            lam = 0;
        }
        hare = (*f)(hare);
        lam++;
    }

    hare = x0;
    i = 0;
    while (i < lam) {
        hare = (*f)(hare);
        i++;
    }

    tortoise = x0;
    mu = 0;
    while (tortoise != hare) {
        tortoise = (*f)(tortoise);
        hare = (*f)(hare);
        mu++;
    }

    result.a = lam;
    result.b = mu;
    return result;
}

int lambda(int x) {
    return (x*x + 1) % 255;
}

int main() {
    int x0 = 3, x = 3, i;
    Pair result;

    printf("[3");
    for (i = 1; i <= 40; ++i) {
        x = lambda(x);
        printf(", %d", x);
    }
    printf("]\n");

    result = brent(lambda, x0);
    printf("Cycle length = %d\nStart index = %d\nCycle = [", result.a, result.b);

    x0 = 3;
    x = x0;
    for (i = 1; i <= result.b; ++i) {
        x = lambda(x);
    }
    for (i = 1; i <= result.a; ++i) {
        if (i > 1) {
            printf(", ");
        }

        printf("%d", x);
        x = lambda(x);
    }

    printf("]\n");

    return 0;
}

VB: Module Module1

    Function FindCycle(Of T As IEquatable(Of T))(x0 As T, yielder As Func(Of T, T)) As Tuple(Of Integer, Integer)
        Dim power = 1
        Dim lambda = 1
        Dim tortoise As T
        Dim hare As T

        tortoise = x0
        hare = yielder(x0)

        
        While Not tortoise.Equals(hare)
            If power = lambda Then
                tortoise = hare
                power *= 2
                lambda = 0
            End If
            hare = yielder(hare)
            lambda += 1
        End While

        
        Dim mu = 0
        tortoise = x0
        hare = x0
        For times = 1 To lambda
            hare = yielder(hare)
        Next

        While Not tortoise.Equals(hare)
            tortoise = yielder(tortoise)
            hare = yielder(hare)
            mu += 1
        End While

        Return Tuple.Create(lambda, mu)
    End Function

    Sub Main()
        
        Dim sequence = Function(_x As Integer) (_x * _x + 1) Mod 255

        
        Dim x = 3
        Console.Write(x)
        For times = 0 To 39
            x = sequence(x)
            Console.Write(",{0}", x)
        Next
        Console.WriteLine()

        
        Dim cycle = FindCycle(3, sequence)
        Console.WriteLine("Cycle length = {0}", cycle.Item1)
        Console.WriteLine("Start index = {0}", cycle.Item2)
    End Sub

End Module