WeixiangYan/CodeTransOcean huggingface dataset:
Task: Numbers with same digit set in base 10 and base 16


C++: #include <iostream>
#include <iomanip>
#include <bitset>

const int LIMIT = 100000;

std::bitset<16> digitset(int num, int base) {
    std::bitset<16> set;
    for (; num; num /= base) set.set(num % base);
    return set;
}

int main() {
    int c = 0;
    for (int i=0; i<LIMIT; i++) {
        if (digitset(i,10) == digitset(i,16)) {
            std::cout << std::setw(7) << i;
            if (++c % 10 == 0) std::cout << std::endl;
        }
    }
    std::cout << std::endl;
    return 0;
}
Task: Matrix with two diagonals


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



void PrintMatrix(std::predicate<int, int, int> auto f, int size)
{
  for(int y = 0; y < size; y++)
  {
    for(int x = 0; x < size; x++)
    {
      std::cout << " " << f(x, y, size);
    }
    std::cout << "\n";
  }
  std::cout << "\n";
}

int main()
{
  
  auto diagonals = [](int x, int y, int size)
  {
    return x == y || ((size - x - 1) == y);
  };
  
  PrintMatrix(diagonals, 8);
  PrintMatrix(diagonals, 9);
}
Task: Four sides of square


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



void PrintMatrix(std::predicate<int, int, int> auto f, int size)
{
  for(int y = 0; y < size; y++)
  {
    for(int x = 0; x < size; x++)
    {
      std::cout << " " << f(x, y, size);
    }
    std::cout << "\n";
  }
  std::cout << "\n";
}

int main()
{
  
  auto fourSides = [](int x, int y, int size)
  {
    return x == 0 || (y == 0) || (x == size - 1) || (y == size - 1);
  };

  PrintMatrix(fourSides, 8);
  PrintMatrix(fourSides, 9);
}
Task: Longest common suffix


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

std::string lcs(const std::vector<std::string>& strs) {
    std::vector<std::string::const_reverse_iterator> backs;
    std::string s;
    
    if (strs.size() == 0) return "";
    if (strs.size() == 1) return strs[0];
    
    for (auto& str : strs) backs.push_back(str.crbegin());
    
    while (backs[0] != strs[0].crend()) {
        char ch = *backs[0]++;
        for (std::size_t i = 1; i<strs.size(); i++) {
            if (backs[i] == strs[i].crend()) goto done;
            if (*backs[i] != ch) goto done;
            backs[i]++;
        }
        s.push_back(ch);
    }
    
done:
    reverse(s.begin(), s.end());
    return s;
}

void test(const std::vector<std::string>& strs) {
    std::cout << "[";
    for (std::size_t i = 0; i<strs.size(); i++) {
        std::cout << '"' << strs[i] << '"';
        if (i != strs.size()-1) std::cout << ", ";
    }
    std::cout << "] -> `" << lcs(strs) << "`\n";
}

int main() {
    std::vector<std::string> t1 = {"baabababc", "baabc", "bbabc"};
    std::vector<std::string> t2 = {"baabababc", "baabc", "bbazc"};
    std::vector<std::string> t3 = 
        {"Sunday", "Monday", "Tuesday", "Wednesday", "Friday", "Saturday"};
    std::vector<std::string> t4 = {"longest", "common", "suffix"};
    std::vector<std::string> t5 = {""};
    std::vector<std::string> t6 = {};
    std::vector<std::string> t7 = {"foo", "foo", "foo", "foo"};

    std::vector<std::vector<std::string>> tests = {t1,t2,t3,t4,t5,t6,t7};
    
    for (auto t : tests) test(t);
    return 0;
}
Task: Unique characters


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

int main() {
    const char* strings[] = {"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"};
    std::map<char, int> count;
    for (const char* str : strings) {
        for (; *str; ++str)
            ++count[*str];
    }
    for (const auto& p : count) {
        if (p.second == 1)
            std::cout << p.first;
    }
    std::cout << '\n';
}
Task: Sequence_ smallest number with exactly n divisors


C++: #include <iostream>

#define MAX 15

using namespace std;

int count_divisors(int n) {
    int count = 0;
    for (int i = 1; i * i <= n; ++i) {
        if (!(n % i)) {
            if (i == n / i)
                count++;
            else
                count += 2;
        }
    }
    return count;
}

int main() {
    int i, k, n, seq[MAX];
    for (i = 0; i < MAX; ++i) seq[i] = 0;
    cout << "The first " << MAX << " terms of the sequence are:" << endl;
    for (i = 1, n = 0; n <  MAX; ++i) {
        k = count_divisors(i);
        if (k <= MAX && seq[k - 1] == 0) {
            seq[k - 1] = i;
            ++n;
        }
    }
    for (i = 0; i < MAX; ++i) cout << seq[i] << " ";
    cout << endl;
    return 0;
}
Task: Palindromic primes


C++: #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

unsigned int reverse(unsigned int base, unsigned int n) {
    unsigned int rev = 0;
    for (; n > 0; n /= base)
        rev = rev * base + (n % base);
    return rev;
}

class palindrome_generator {
public:
    explicit palindrome_generator(unsigned int base)
        : base_(base), upper_(base) {}
    unsigned int next_palindrome();

private:
    unsigned int base_;
    unsigned int lower_ = 1;
    unsigned int upper_;
    unsigned int next_ = 0;
    bool even_ = false;
};

unsigned int palindrome_generator::next_palindrome() {
    ++next_;
    if (next_ == upper_) {
        if (even_) {
            lower_ = upper_;
            upper_ *= base_;
        }
        next_ = lower_;
        even_ = !even_;
    }
    return even_ ? next_ * upper_ + reverse(base_, next_)
                 : next_ * lower_ + reverse(base_, next_ / base_);
}

bool is_prime(unsigned int n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (unsigned int p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

std::string to_string(unsigned int base, unsigned int n) {
    assert(base <= 36);
    static constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    std::string str;
    for (; n != 0; n /= base)
        str += digits[n % base];
    std::reverse(str.begin(), str.end());
    return str;
}

void print_palindromic_primes(unsigned int base, unsigned int limit) {
    auto width =
        static_cast<unsigned int>(std::ceil(std::log(limit) / std::log(base)));
    unsigned int count = 0;
    auto columns = 80 / (width + 1);
    std::cout << "Base " << base << " palindromic primes less than " << limit
              << ":\n";
    palindrome_generator pgen(base);
    unsigned int palindrome;
    while ((palindrome = pgen.next_palindrome()) < limit) {
        if (is_prime(palindrome)) {
            ++count;
            std::cout << std::setw(width) << to_string(base, palindrome)
                      << (count % columns == 0 ? '\n' : ' ');
        }
    }
    if (count % columns != 0)
        std::cout << '\n';
    std::cout << "Count: " << count << '\n';
}

void count_palindromic_primes(unsigned int base, unsigned int limit) {
    unsigned int count = 0;
    palindrome_generator pgen(base);
    unsigned int palindrome;
    while ((palindrome = pgen.next_palindrome()) < limit)
        if (is_prime(palindrome))
            ++count;
    std::cout << "Number of base " << base << " palindromic primes less than "
              << limit << ": " << count << '\n';
}

int main() {
    print_palindromic_primes(10, 1000);
    std::cout << '\n';
    print_palindromic_primes(10, 100000);
    std::cout << '\n';
    count_palindromic_primes(10, 1000000000);
    std::cout << '\n';
    print_palindromic_primes(16, 500);
}
Task: Sylvester's sequence


C++: #include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using integer = boost::multiprecision::cpp_int;
using rational = boost::rational<integer>;

integer sylvester_next(const integer& n) {
    return n * n - n + 1;
}

int main() {
    std::cout << "First 10 elements in Sylvester's sequence:\n";
    integer term = 2;
    rational sum = 0;
    for (int i = 1; i <= 10; ++i) {
        std::cout << std::setw(2) << i << ": " << term << '\n';
        sum += rational(1, term);
        term = sylvester_next(term);
    }
    std::cout << "Sum of reciprocals: " << sum << '\n';
}
Task: Getting the number of decimal places


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

int findNumOfDec(const char *s) {
    int pos = 0;
    while (s[pos] && s[pos++] != '.') {}
    return strlen(s + pos);
}

void test(const char *s) {
    int num = findNumOfDec(s);
    const char *p  = num != 1 ? "s" : "";
    std::cout << s << " has " << num << " decimal" << p << "\n";
}

int main() {
    test("12");
    test("12.0");
    test("12.345");
    test("12.345555555555");
    test("12.3450");
    test("12.34555555555555555555");
    char str[64];
    sprintf(str, "%f", 1.2345e+54);
    test(str);
    return 0;
}
Task: Mersenne primes


C++: #include <iostream>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    if (n % 3 == 0) return n == 3;

    uint64_t test = 5;
    while (test * test < n) {
        if (n % test == 0) return false;
        test += 2;
        if (n % test == 0) return false;
        test += 4;
    }

    return true;
}

int main() {
    uint64_t base = 2;

    for (int pow = 1; pow <= 32; pow++) {
        if (isPrime(base - 1)) {
            std::cout << "2 ^ " << pow << " - 1\n";
        }
        base *= 2;
    }

    return 0;
}
Task: Pancake numbers


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

int pancake(int n) {
    int gap = 2, sum = 2, adj = -1;
    while (sum < n) {
        adj++;
        gap = gap * 2 - 1;
        sum += gap;
    }
    return n + adj;
}

int main() {
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j < 6; j++) {
            int n = i * 5 + j;
            std::cout << "p(" << std::setw(2) << n << ") = " << std::setw(2) << pancake(n) << "  ";
        }
        std::cout << '\n';
    }
    return 0;
}
Task: Steady squares


C++: #include <iostream>
using namespace std;

bool steady(int n) {
    int mask = 1;
    for (int d = n; d != 0; d /= 10) 
        mask *= 10;
    return (n * n) % mask == n;
}
 
int main() {
    for (int i = 1; i < 10000; i++)
        if (steady(i)) printf("%4d^2 = %8d\n", i, i * i);
}
Task: Substring primes


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

std::vector<bool> prime_sieve(size_t limit) {
    std::vector<bool> sieve(limit, true);
    if (limit > 0)
        sieve[0] = false;
    if (limit > 1)
        sieve[1] = false;
    for (size_t i = 4; i < limit; i += 2)
        sieve[i] = false;
    for (size_t p = 3; ; p += 2) {
        size_t q = p * p;
        if (q >= limit)
            break;
        if (sieve[p]) {
            size_t inc = 2 * p;
            for (; q < limit; q += inc)
                sieve[q] = false;
        }
    }
    return sieve;
}

bool substring_prime(const std::vector<bool>& sieve, unsigned int n) {
    for (; n != 0; n /= 10) {
        if (!sieve[n])
            return false;
        for (unsigned int p = 10; p < n; p *= 10) {
            if (!sieve[n % p])
                return false;
        }
    }
    return true;
}

int main() {
    const unsigned int limit = 500;
    std::vector<bool> sieve = prime_sieve(limit);
    for (unsigned int i = 2; i < limit; ++i) {
        if (substring_prime(sieve, i))
            std::cout << i << '\n';
    }
    return 0;
}
Task: Special pythagorean triplet


C++: #include <cmath>
#include <concepts>
#include <iostream>
#include <numeric>
#include <optional>
#include <tuple>
 
using namespace std;

optional<tuple<int, int ,int>> FindPerimeterTriplet(int perimeter)
{
    unsigned long long perimeterULL = perimeter;
    auto max_M = (unsigned long long)sqrt(perimeter/2) + 1;
    for(unsigned long long m = 2; m < max_M; ++m)
    {
        for(unsigned long long n = 1 + m % 2; n < m; n+=2)
        {
            if(gcd(m,n) != 1)
            {
                continue;
            }

            
            
            
            
 
            auto a = m * m - n * n;
            auto b = 2 * m * n;
            auto c = m * m + n * n;
            auto primitive = a + b + c;

            
            auto factor = perimeterULL / primitive;
            if(primitive * factor == perimeterULL)
            {
              
              if(b<a) swap(a, b);
              return tuple{a * factor, b * factor, c * factor};
            }
        }
    }

    
    return nullopt;
}
 
int main()
{
  auto t1 = FindPerimeterTriplet(1000);
  if(t1)
  {
    auto [a, b, c] = *t1;
    cout << "[" << a << ", " << b << ", " << c << "]\n";
    cout << "a * b * c = " << a * b * c << "\n";
  }
  else
  {
    cout << "Perimeter not found\n";
  }
}
Task: Arena storage pool




Go: package main

import (
    "fmt"
    "runtime"
    "sync"
)





func main() {
    
    
    p := sync.Pool{New: func() interface{} {
        fmt.Println("pool empty")
        return new(int)
    }}
    
    i := new(int)
    j := new(int)
    
    *i = 1
    *j = 2
    fmt.Println(*i + *j) 
    
    
    
    
    p.Put(i)
    p.Put(j)
    
    
    i = nil
    j = nil
    
    
    
    i = p.Get().(*int)
    j = p.Get().(*int)
    *i = 4
    *j = 5
    fmt.Println(*i + *j) 
    
    p.Put(i)
    p.Put(j)
    i = nil
    j = nil
    runtime.GC()
    i = p.Get().(*int)
    j = p.Get().(*int)
    *i = 7
    *j = 8
    fmt.Println(*i + *j) 
}
Task: Set right-adjacent bits




Go: package main

import (
    "fmt"
    "strings"
)

type test struct {
    bs string
    n  int
}

func setRightBits(bits []byte, e, n int) []byte {
    if e == 0 || n <= 0 {
        return bits
    }
    bits2 := make([]byte, len(bits))
    copy(bits2, bits)
    for i := 0; i < e-1; i++ {
        c := bits[i]
        if c == 1 {
            j := i + 1
            for j <= i+n && j < e {
                bits2[j] = 1
                j++
            }
        }
    }
    return bits2
}

func main() {
    b := "010000000000100000000010000000010000000100000010000010000100010010"
    tests := []test{
        test{"1000", 2}, test{"0100", 2}, test{"0010", 2}, test{"0000", 2},
        test{b, 0}, test{b, 1}, test{b, 2}, test{b, 3},
    }
    for _, test := range tests {
        bs := test.bs
        e := len(bs)
        n := test.n
        fmt.Println("n =", n, "\b; Width e =", e, "\b:")
        fmt.Println("    Input b:", bs)
        bits := []byte(bs)
        for i := 0; i < len(bits); i++ {
            bits[i] = bits[i] - '0'
        }
        bits = setRightBits(bits, e, n)
        var sb strings.Builder
        for i := 0; i < len(bits); i++ {
            sb.WriteByte(bits[i] + '0')
        }
        fmt.Println("    Result :", sb.String())
    }
}
Task: Positive decimal integers with the digit 1 occurring exactly twice




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    fmt.Println("Decimal numbers under 1,000 whose digits include two 1's:")
    var results []int
    for i := 11; i <= 911; i++ {
        digits := rcu.Digits(i, 10)
        count := 0
        for _, d := range digits {
            if d == 1 {
                count++
            }
        }
        if count == 2 {
            results = append(results, i)
        }
    }
    for i, n := range results {
        fmt.Printf("%5d", n)
        if (i+1)%7 == 0 {
            fmt.Println()
        }
    }
    fmt.Println("\n\nFound", len(results), "such numbers.")
}
Task: Unique characters in each string




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    strings := []string{"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"}
    u := make(map[rune]int)
    for _, s := range strings {
        m := make(map[rune]int)
        for _, c := range s {
            m[c]++
        }
        for k, v := range m {
            if v == 1 {
                u[k]++
            }
        }
    }
    var chars []rune
    for k, v := range u {
        if v == 3 {
            chars = append(chars, k)
        }
    }
    sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
    fmt.Println(string(chars))
}
Task: Jacobsthal numbers




Go: package main

import (
    "fmt"
    "math/big"
)

func jacobsthal(n uint) *big.Int {
    t := big.NewInt(1)
    t.Lsh(t, n)
    s := big.NewInt(1)
    if n%2 != 0 {
        s.Neg(s)
    }
    t.Sub(t, s)
    return t.Div(t, big.NewInt(3))
}

func jacobsthalLucas(n uint) *big.Int {
    t := big.NewInt(1)
    t.Lsh(t, n)
    a := big.NewInt(1)
    if n%2 != 0 {
        a.Neg(a)
    }
    return t.Add(t, a)
}

func main() {
    jac := make([]*big.Int, 30)
    fmt.Println("First 30 Jacobsthal numbers:")
    for i := uint(0); i < 30; i++ {
        jac[i] = jacobsthal(i)
        fmt.Printf("%9d ", jac[i])
        if (i+1)%5 == 0 {
            fmt.Println()
        }
    }

    fmt.Println("\nFirst 30 Jacobsthal-Lucas numbers:")
    for i := uint(0); i < 30; i++ {
        fmt.Printf("%9d ", jacobsthalLucas(i))
        if (i+1)%5 == 0 {
            fmt.Println()
        }
    }

    fmt.Println("\nFirst 20 Jacobsthal oblong numbers:")
    for i := uint(0); i < 20; i++ {
        t := big.NewInt(0)
        fmt.Printf("%11d ", t.Mul(jac[i], jac[i+1]))
        if (i+1)%5 == 0 {
            fmt.Println()
        }
    }

    fmt.Println("\nFirst 20 Jacobsthal primes:")
    for n, count := uint(0), 0; count < 20; n++ {
        j := jacobsthal(n)
        if j.ProbablyPrime(10) {
            fmt.Println(j)
            count++
        }
    }
}
Task: Successive prime differences




Go: package main

import "fmt"

func sieve(limit int) []int {
    primes := []int{2}
    c := make([]bool, limit+1) 
    
    p := 3
    for {
        p2 := p * p
        if p2 > limit {
            break
        }
        for i := p2; i <= limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    for i := 3; i <= limit; i += 2 {
        if !c[i] {
            primes = append(primes, i)
        }
    }
    return primes
}

func successivePrimes(primes, diffs []int) [][]int {
    var results [][]int
    dl := len(diffs)
outer:
    for i := 0; i < len(primes)-dl; i++ {
        group := make([]int, dl+1)
        group[0] = primes[i]
        for j := i; j < i+dl; j++ {
            if primes[j+1]-primes[j] != diffs[j-i] {
                group = nil
                continue outer
            }
            group[j-i+1] = primes[j+1]
        }
        results = append(results, group)
        group = nil
    }
    return results
}

func main() {
    primes := sieve(999999)
    diffsList := [][]int{{2}, {1}, {2, 2}, {2, 4}, {4, 2}, {6, 4, 2}}
    fmt.Println("For primes less than 1,000,000:-\n")
    for _, diffs := range diffsList {
        fmt.Println("  For differences of", diffs, "->")
        sp := successivePrimes(primes, diffs)
        if len(sp) == 0 {
            fmt.Println("    No groups found")
            continue
        }
        fmt.Println("    First group   = ", sp[0])
        fmt.Println("    Last group    = ", sp[len(sp)-1])
        fmt.Println("    Number found  = ", len(sp))
        fmt.Println()
    }
}
Task: Numbers whose binary and ternary digit sums are prime




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    var numbers []int
    for i := 2; i < 200; i++ {
        bds := rcu.DigitSum(i, 2)
        if rcu.IsPrime(bds) {
            tds := rcu.DigitSum(i, 3)
            if rcu.IsPrime(tds) {
                numbers = append(numbers, i)
            }
        }
    }
    fmt.Println("Numbers < 200 whose binary and ternary digit sums are prime:")
    for i, n := range numbers {
        fmt.Printf("%4d", n)
        if (i+1)%14 == 0 {
            fmt.Println()
        }
    }
    fmt.Printf("\n\n%d such numbers found\n", len(numbers))
}
Task: 10001th prime




Go: package main

import "fmt"

func isPrime(n int) bool {
    if n == 1 {
        return false
    }
    i := 2
    for i*i <= n {
        if n%i == 0 {
            return false
        }
        i++
    }
    return true
}

func main() {
    var final, pNum int

    for i := 1; pNum < 10001; i++ {
        if isPrime(i) {
            pNum++
        }
        final = i
    }
    fmt.Println(final)
}
Task: Summarize primes




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    primes := rcu.Primes(999)
    sum, n, c := 0, 0, 0
    fmt.Println("Summing the first n primes (<1,000) where the sum is itself prime:")
    fmt.Println("  n  cumulative sum")
    for _, p := range primes {
        n++
        sum += p
        if rcu.IsPrime(sum) {
            c++
            fmt.Printf("%3d   %6s\n", n, rcu.Commatize(sum))
        }
    }
    fmt.Println()
    fmt.Println(c, "such prime sums found")
}
Task: Terminal control_Dimensions




Go: package main

import (
    "fmt"
    "os"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    w, h, err := terminal.GetSize(int(os.Stdout.Fd()))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(h, w)
}
Task: Next special primes




Go: package main

import "fmt"

func sieve(limit int) []bool {
    limit++
    
    c := make([]bool, limit) 
    c[0] = true
    c[1] = true
    
    p := 3 
    for {
        p2 := p * p
        if p2 >= limit {
            break
        }
        for i := p2; i < limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    return c
}

func main() {
    c := sieve(1049)
    fmt.Println("Special primes under 1,050:")
    fmt.Println("Prime1 Prime2 Gap")
    lastSpecial := 3
    lastGap := 1
    fmt.Printf("%6d %6d %3d\n", 2, 3, lastGap)
    for i := 5; i < 1050; i += 2 {
        if !c[i] && (i-lastSpecial) > lastGap {
            lastGap = i - lastSpecial
            fmt.Printf("%6d %6d %3d\n", lastSpecial, i, lastGap)
            lastSpecial = i
        }
    }
}
Task: Largest number divisible by its digits




Go: package main

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

func divByAll(num int, digits []byte) bool {
    for _, digit := range digits {
        if num%int(digit-'0') != 0 {
            return false
        }
    }
    return true
}

func main() {
    magic := 9 * 8 * 7
    high := 9876432 / magic * magic
    for i := high; i >= magic; i -= magic {
        if i%10 == 0 {
            continue 
        }
        s := strconv.Itoa(i)
        if strings.ContainsAny(s, "05") {
            continue 
        }
        var set = make(map[byte]bool)
        var sd []byte 
        for _, b := range []byte(s) {
            if !set[b] {
                set[b] = true
                sd = append(sd, b)
            }
        }
        if len(sd) != len(s) {
            continue 
        }
        if divByAll(i, sd) {
            fmt.Println("Largest decimal number is", i)
            return
        }
    }
}
Task: Jacobi symbol




Go: package main

import (
    "fmt"
    "log"
    "math/big"
)

func jacobi(a, n uint64) int {
    if n%2 == 0 {
        log.Fatal("'n' must be a positive odd integer")
    }
    a %= n
    result := 1
    for a != 0 {
        for a%2 == 0 {
            a /= 2
            nn := n % 8
            if nn == 3 || nn == 5 {
                result = -result
            }
        }
        a, n = n, a
        if a%4 == 3 && n%4 == 3 {
            result = -result
        }
        a %= n
    }
    if n == 1 {
        return result
    }
    return 0
}

func main() {
    fmt.Println("Using hand-coded version:")
    fmt.Println("n/a  0  1  2  3  4  5  6  7  8  9")
    fmt.Println("---------------------------------")
    for n := uint64(1); n <= 17; n += 2 {
        fmt.Printf("%2d ", n)
        for a := uint64(0); a <= 9; a++ {
            fmt.Printf(" % d", jacobi(a, n))
        }
        fmt.Println()
    }

    ba, bn := new(big.Int), new(big.Int)
    fmt.Println("\nUsing standard library function:")
    fmt.Println("n/a  0  1  2  3  4  5  6  7  8  9")
    fmt.Println("---------------------------------")
    for n := uint64(1); n <= 17; n += 2 {
        fmt.Printf("%2d ", n)
        for a := uint64(0); a <= 9; a++ {
            ba.SetUint64(a)
            bn.SetUint64(n)
            fmt.Printf(" % d", big.Jacobi(ba, bn))            
        }
        fmt.Println()
    }
}