WeixiangYan/CodeTransOcean huggingface dataset:
Task: Integer roots







Python: def root(a, b):
    if b < 2:
        return b
    a1 = a - 1
    c = 1
    d = (a1 * c + b // (c ** a1)) // a
    e = (a1 * d + b // (d ** a1)) // a
    while c not in (d, e):
        c, d, e = d, e, (a1 * e + b // (e ** a1)) // a
    return min(d, e)


print("First 2,001 digits of the square root of two:\n{}".format(
    root(2, 2 * 100 ** 2000)
))


VB: Imports System
Imports System.Numerics
Imports Microsoft.VisualBasic.Strings

Public Module Module1

    Public Function IntSqRoot(v As BigInteger) As BigInteger
        Dim digs As Integer = Math.Max(0, v.ToString().Length / 2 - 1)
        IntSqRoot = BigInteger.Parse("3" & StrDup(digs, "0"))
        Dim term As BigInteger
        Do
            term = v / IntSqRoot
            If Math.Abs(CDbl(term - IntSqRoot)) < 2 Then Exit Do
            IntSqRoot = (IntSqRoot + term) / 2
        Loop Until False
    End Function

    Public Function IntNthRoot(n As Integer, v As BigInteger) As BigInteger
        Dim digs As Integer = Math.Max(0, v.ToString().Length / n - 1)
        IntNthRoot = BigInteger.Parse(If(digs > 1, 3, 2).ToString() & StrDup(digs, "0"))
        Dim va As BigInteger, dr As BigInteger
        Do
            va = v : For i As Integer = 2 To n : va /= IntNthRoot : Next
            va -= IntNthRoot
            dr = va / n : If dr = 0 Then Exit Do
            IntNthRoot += dr
        Loop Until False
    End Function

    Public Sub Main()
        Dim b As BigInteger = BigInteger.Parse("2" & StrDup(4008, "0"))
        Console.WriteLine("Integer Cube Root of 8:")
        Console.WriteLine(IntNthRoot(3, 8).ToString()) 
        Console.WriteLine("Integer Cube Root of 9:")
        Console.WriteLine(IntNthRoot(3, 9).ToString()) 
        Console.WriteLine("Integer Square Root of 2, (actually 2 * 10 ^ 4008, square root method):")
        Console.WriteLine(IntSqRoot(b).ToString()) 
        Console.WriteLine("Integer Square Root of 2, (actually 2 * 10 ^ 4008, nth root method):")
        Console.WriteLine(IntNthRoot(2, b).ToString()) 
        Console.WriteLine("Integer Cube Root of 2, (actually 2 * 10 ^ 4008):")
        Console.WriteLine(IntNthRoot(3, b).ToString()) 
        b /= 10000
        Console.WriteLine("Integer 7th Root of 2, (actually 2 * 10 ^ 4004):")
        Console.WriteLine(IntNthRoot(7, b).ToString()) 
        If Diagnostics.Debugger.IsAttached Then Console.Read()
    End Sub

End Module
Task: Integer roots

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

typedef unsigned long long ulong;

ulong root(ulong base, ulong n) {
    ulong n1, n2, n3, c, d, e;

    if (base < 2) return base;
    if (n == 0) return 1;

    n1 = n - 1;
    n2 = n;
    n3 = n1;
    c = 1;
    d = (n3 + base) / n2;
    e = (n3 * d + base / (ulong)powl(d, n1)) / n2;

    while (c != d && c != e) {
        c = d;
        d = e;
        e = (n3*e + base / (ulong)powl(e, n1)) / n2;
    }

    if (d < e) return d;
    return e;
}

int main() {
    ulong b = (ulong)2e18;

    printf("3rd root of 8 = %lld\n", root(8, 3));
    printf("3rd root of 9 = %lld\n", root(9, 3));
    printf("2nd root of %lld = %lld\n", b, root(b, 2));

    return 0;
}







Python: def root(a, b):
    if b < 2:
        return b
    a1 = a - 1
    c = 1
    d = (a1 * c + b // (c ** a1)) // a
    e = (a1 * d + b // (d ** a1)) // a
    while c not in (d, e):
        c, d, e = d, e, (a1 * e + b // (e ** a1)) // a
    return min(d, e)


print("First 2,001 digits of the square root of two:\n{}".format(
    root(2, 2 * 100 ** 2000)
))
Task: Super-d numbers

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

int main() {
    for (unsigned int d = 2; d <= 9; ++d) {
        printf("First 10 super-%u numbers:\n", d);
        char digits[16] = { 0 };
        memset(digits, '0' + d, d);
        mpz_t bignum;
        mpz_init(bignum);
        for (unsigned int count = 0, n = 1; count < 10; ++n) {
            mpz_ui_pow_ui(bignum, n, d);
            mpz_mul_ui(bignum, bignum, d);
            char* str = mpz_get_str(NULL, 10, bignum);
            if (strstr(str, digits)) {
                printf("%u ", n);
                ++count;
            }
            free(str);
        }
        mpz_clear(bignum);
        printf("\n");
    }
    return 0;
}


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}
Task: Super-d numbers

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

int main() {
    for (unsigned int d = 2; d <= 9; ++d) {
        printf("First 10 super-%u numbers:\n", d);
        char digits[16] = { 0 };
        memset(digits, '0' + d, d);
        mpz_t bignum;
        mpz_init(bignum);
        for (unsigned int count = 0, n = 1; count < 10; ++n) {
            mpz_ui_pow_ui(bignum, n, d);
            mpz_mul_ui(bignum, bignum, d);
            char* str = mpz_get_str(NULL, 10, bignum);
            if (strstr(str, digits)) {
                printf("%u ", n);
                ++count;
            }
            free(str);
        }
        mpz_clear(bignum);
        printf("\n");
    }
    return 0;
}




Go: package main

import (
    "fmt"
    "math/big"
    "strings"
    "time"
)

func main() {
    start := time.Now()
    rd := []string{"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
    one := big.NewInt(1)
    nine := big.NewInt(9)
    for i := big.NewInt(2); i.Cmp(nine) <= 0; i.Add(i, one) {
        fmt.Printf("First 10 super-%d numbers:\n", i)
        ii := i.Uint64()
        k := new(big.Int)
        count := 0
    inner:
        for j := big.NewInt(3); ; j.Add(j, one) {
            k.Exp(j, i, nil)
            k.Mul(i, k)
            ix := strings.Index(k.String(), rd[ii-2])
            if ix >= 0 {
                count++
                fmt.Printf("%d ", j)
                if count == 10 {
                    fmt.Printf("\nfound in %d ms\n\n", time.Since(start).Milliseconds())
                    break inner
                }
            }
        }
    }
}
Task: Super-d numbers

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

int main() {
    for (unsigned int d = 2; d <= 9; ++d) {
        printf("First 10 super-%u numbers:\n", d);
        char digits[16] = { 0 };
        memset(digits, '0' + d, d);
        mpz_t bignum;
        mpz_init(bignum);
        for (unsigned int count = 0, n = 1; count < 10; ++n) {
            mpz_ui_pow_ui(bignum, n, d);
            mpz_mul_ui(bignum, bignum, d);
            char* str = mpz_get_str(NULL, 10, bignum);
            if (strstr(str, digits)) {
                printf("%u ", n);
                ++count;
            }
            free(str);
        }
        mpz_clear(bignum);
        printf("\n");
    }
    return 0;
}





Java: import java.math.BigInteger;

public class SuperDNumbers {

    public static void main(String[] args) {
        for ( int i = 2 ; i <= 9 ; i++ ) {
            superD(i, 10);
        }
    }
    
    private static final void superD(int d, int max) {
        long start = System.currentTimeMillis();
        String test = "";
        for ( int i = 0 ; i < d ; i++ ) {
            test += (""+d);
        }
        
        int n = 0;
        int i = 0;
        System.out.printf("First %d super-%d numbers: %n", max, d);
        while ( n < max ) {
            i++;
            BigInteger val = BigInteger.valueOf(d).multiply(BigInteger.valueOf(i).pow(d));
            if ( val.toString().contains(test) ) {
                n++;
                System.out.printf("%d ", i);
            }
        }
        long end = System.currentTimeMillis();
        System.out.printf("%nRun time %d ms%n%n", end-start);
        
    }

}
Task: Super-d numbers

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

int main() {
    for (unsigned int d = 2; d <= 9; ++d) {
        printf("First 10 super-%u numbers:\n", d);
        char digits[16] = { 0 };
        memset(digits, '0' + d, d);
        mpz_t bignum;
        mpz_init(bignum);
        for (unsigned int count = 0, n = 1; count < 10; ++n) {
            mpz_ui_pow_ui(bignum, n, d);
            mpz_mul_ui(bignum, bignum, d);
            char* str = mpz_get_str(NULL, 10, bignum);
            if (strstr(str, digits)) {
                printf("%u ", n);
                ++count;
            }
            free(str);
        }
        mpz_clear(bignum);
        printf("\n");
    }
    return 0;
}







Python: from itertools import islice, count

def superd(d):
    if d != int(d) or not 2 <= d <= 9:
        raise ValueError("argument must be integer from 2 to 9 inclusive")
    tofind = str(d) * d
    for n in count(2):
        if tofind in str(d * n ** d):
            yield n

if __name__ == '__main__':
    for d in range(2, 9):
        print(f"{d}:", ', '.join(str(n) for n in islice(superd(d), 10)))
Task: Super-d numbers

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

int main() {
    for (unsigned int d = 2; d <= 9; ++d) {
        printf("First 10 super-%u numbers:\n", d);
        char digits[16] = { 0 };
        memset(digits, '0' + d, d);
        mpz_t bignum;
        mpz_init(bignum);
        for (unsigned int count = 0, n = 1; count < 10; ++n) {
            mpz_ui_pow_ui(bignum, n, d);
            mpz_mul_ui(bignum, bignum, d);
            char* str = mpz_get_str(NULL, 10, bignum);
            if (strstr(str, digits)) {
                printf("%u ", n);
                ++count;
            }
            free(str);
        }
        mpz_clear(bignum);
        printf("\n");
    }
    return 0;
}








VB: Imports System.Numerics

Module Module1

    Sub Main()
        Dim rd = {"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
        Dim one As BigInteger = 1
        Dim nine As BigInteger = 9

        For ii = 2 To 9
            Console.WriteLine("First 10 super-{0} numbers:", ii)
            Dim count = 0

            Dim j As BigInteger = 3
            While True
                Dim k = ii * BigInteger.Pow(j, ii)
                Dim ix = k.ToString.IndexOf(rd(ii - 2))
                If ix >= 0 Then
                    count += 1
                    Console.Write("{0} ", j)
                    If count = 10 Then
                        Console.WriteLine()
                        Console.WriteLine()
                        Exit While
                    End If
                End If

                j += 1
            End While
        Next
    End Sub

End Module
Task: Super-d numbers


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}



Go: package main

import (
    "fmt"
    "math/big"
    "strings"
    "time"
)

func main() {
    start := time.Now()
    rd := []string{"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
    one := big.NewInt(1)
    nine := big.NewInt(9)
    for i := big.NewInt(2); i.Cmp(nine) <= 0; i.Add(i, one) {
        fmt.Printf("First 10 super-%d numbers:\n", i)
        ii := i.Uint64()
        k := new(big.Int)
        count := 0
    inner:
        for j := big.NewInt(3); ; j.Add(j, one) {
            k.Exp(j, i, nil)
            k.Mul(i, k)
            ix := strings.Index(k.String(), rd[ii-2])
            if ix >= 0 {
                count++
                fmt.Printf("%d ", j)
                if count == 10 {
                    fmt.Printf("\nfound in %d ms\n\n", time.Since(start).Milliseconds())
                    break inner
                }
            }
        }
    }
}
Task: Super-d numbers


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}




Java: import java.math.BigInteger;

public class SuperDNumbers {

    public static void main(String[] args) {
        for ( int i = 2 ; i <= 9 ; i++ ) {
            superD(i, 10);
        }
    }
    
    private static final void superD(int d, int max) {
        long start = System.currentTimeMillis();
        String test = "";
        for ( int i = 0 ; i < d ; i++ ) {
            test += (""+d);
        }
        
        int n = 0;
        int i = 0;
        System.out.printf("First %d super-%d numbers: %n", max, d);
        while ( n < max ) {
            i++;
            BigInteger val = BigInteger.valueOf(d).multiply(BigInteger.valueOf(i).pow(d));
            if ( val.toString().contains(test) ) {
                n++;
                System.out.printf("%d ", i);
            }
        }
        long end = System.currentTimeMillis();
        System.out.printf("%nRun time %d ms%n%n", end-start);
        
    }

}
Task: Super-d numbers


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}






Python: from itertools import islice, count

def superd(d):
    if d != int(d) or not 2 <= d <= 9:
        raise ValueError("argument must be integer from 2 to 9 inclusive")
    tofind = str(d) * d
    for n in count(2):
        if tofind in str(d * n ** d):
            yield n

if __name__ == '__main__':
    for d in range(2, 9):
        print(f"{d}:", ', '.join(str(n) for n in islice(superd(d), 10)))
Task: Super-d numbers


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}







VB: Imports System.Numerics

Module Module1

    Sub Main()
        Dim rd = {"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
        Dim one As BigInteger = 1
        Dim nine As BigInteger = 9

        For ii = 2 To 9
            Console.WriteLine("First 10 super-{0} numbers:", ii)
            Dim count = 0

            Dim j As BigInteger = 3
            While True
                Dim k = ii * BigInteger.Pow(j, ii)
                Dim ix = k.ToString.IndexOf(rd(ii - 2))
                If ix >= 0 Then
                    count += 1
                    Console.Write("{0} ", j)
                    If count = 10 Then
                        Console.WriteLine()
                        Console.WriteLine()
                        Exit While
                    End If
                End If

                j += 1
            End While
        Next
    End Sub

End Module
Task: Super-d numbers




Go: package main

import (
    "fmt"
    "math/big"
    "strings"
    "time"
)

func main() {
    start := time.Now()
    rd := []string{"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
    one := big.NewInt(1)
    nine := big.NewInt(9)
    for i := big.NewInt(2); i.Cmp(nine) <= 0; i.Add(i, one) {
        fmt.Printf("First 10 super-%d numbers:\n", i)
        ii := i.Uint64()
        k := new(big.Int)
        count := 0
    inner:
        for j := big.NewInt(3); ; j.Add(j, one) {
            k.Exp(j, i, nil)
            k.Mul(i, k)
            ix := strings.Index(k.String(), rd[ii-2])
            if ix >= 0 {
                count++
                fmt.Printf("%d ", j)
                if count == 10 {
                    fmt.Printf("\nfound in %d ms\n\n", time.Since(start).Milliseconds())
                    break inner
                }
            }
        }
    }
}


Java: import java.math.BigInteger;

public class SuperDNumbers {

    public static void main(String[] args) {
        for ( int i = 2 ; i <= 9 ; i++ ) {
            superD(i, 10);
        }
    }
    
    private static final void superD(int d, int max) {
        long start = System.currentTimeMillis();
        String test = "";
        for ( int i = 0 ; i < d ; i++ ) {
            test += (""+d);
        }
        
        int n = 0;
        int i = 0;
        System.out.printf("First %d super-%d numbers: %n", max, d);
        while ( n < max ) {
            i++;
            BigInteger val = BigInteger.valueOf(d).multiply(BigInteger.valueOf(i).pow(d));
            if ( val.toString().contains(test) ) {
                n++;
                System.out.printf("%d ", i);
            }
        }
        long end = System.currentTimeMillis();
        System.out.printf("%nRun time %d ms%n%n", end-start);
        
    }

}
Task: Super-d numbers





Java: import java.math.BigInteger;

public class SuperDNumbers {

    public static void main(String[] args) {
        for ( int i = 2 ; i <= 9 ; i++ ) {
            superD(i, 10);
        }
    }
    
    private static final void superD(int d, int max) {
        long start = System.currentTimeMillis();
        String test = "";
        for ( int i = 0 ; i < d ; i++ ) {
            test += (""+d);
        }
        
        int n = 0;
        int i = 0;
        System.out.printf("First %d super-%d numbers: %n", max, d);
        while ( n < max ) {
            i++;
            BigInteger val = BigInteger.valueOf(d).multiply(BigInteger.valueOf(i).pow(d));
            if ( val.toString().contains(test) ) {
                n++;
                System.out.printf("%d ", i);
            }
        }
        long end = System.currentTimeMillis();
        System.out.printf("%nRun time %d ms%n%n", end-start);
        
    }

}




VB: Imports System.Numerics

Module Module1

    Sub Main()
        Dim rd = {"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
        Dim one As BigInteger = 1
        Dim nine As BigInteger = 9

        For ii = 2 To 9
            Console.WriteLine("First 10 super-{0} numbers:", ii)
            Dim count = 0

            Dim j As BigInteger = 3
            While True
                Dim k = ii * BigInteger.Pow(j, ii)
                Dim ix = k.ToString.IndexOf(rd(ii - 2))
                If ix >= 0 Then
                    count += 1
                    Console.Write("{0} ", j)
                    If count = 10 Then
                        Console.WriteLine()
                        Console.WriteLine()
                        Exit While
                    End If
                End If

                j += 1
            End While
        Next
    End Sub

End Module
Task: Super-d numbers




Go: package main

import (
    "fmt"
    "math/big"
    "strings"
    "time"
)

func main() {
    start := time.Now()
    rd := []string{"22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"}
    one := big.NewInt(1)
    nine := big.NewInt(9)
    for i := big.NewInt(2); i.Cmp(nine) <= 0; i.Add(i, one) {
        fmt.Printf("First 10 super-%d numbers:\n", i)
        ii := i.Uint64()
        k := new(big.Int)
        count := 0
    inner:
        for j := big.NewInt(3); ; j.Add(j, one) {
            k.Exp(j, i, nil)
            k.Mul(i, k)
            ix := strings.Index(k.String(), rd[ii-2])
            if ix >= 0 {
                count++
                fmt.Printf("%d ", j)
                if count == 10 {
                    fmt.Printf("\nfound in %d ms\n\n", time.Since(start).Milliseconds())
                    break inner
                }
            }
        }
    }
}




Python: from itertools import islice, count

def superd(d):
    if d != int(d) or not 2 <= d <= 9:
        raise ValueError("argument must be integer from 2 to 9 inclusive")
    tofind = str(d) * d
    for n in count(2):
        if tofind in str(d * n ** d):
            yield n

if __name__ == '__main__':
    for d in range(2, 9):
        print(f"{d}:", ', '.join(str(n) for n in islice(superd(d), 10)))