WeixiangYan/CodeTransOcean huggingface dataset:
Task: Send an unknown method call





Java: import java.lang.reflect.Method;

class Example {
  public int foo(int x) {
    return 42 + x;
  }
}

public class Main {
  public static void main(String[] args) throws Exception {
    Object example = new Example();
    String name = "foo";
    Class<?> clazz = example.getClass();
    Method meth = clazz.getMethod(name, int.class);
    Object result = meth.invoke(example, 5); 
    System.out.println(result);        
  }
}
Task: Factorial primes





Java: public class MainApp {
    public static void main(String[] args) {
        int countOfPrimes = 0;
        final int targetCountOfPrimes = 10;
        long f = 1;
        while (countOfPrimes < targetCountOfPrimes) {
            long factorialNum = getFactorial(f);
            boolean primePlus = isPrime(factorialNum + 1);
            boolean primeMinus = isPrime(factorialNum - 1);
            if (primeMinus) {
                countOfPrimes++;
                System.out.println(countOfPrimes + ": " + factorialNum + "! - 1 = " + (factorialNum - 1));

            }
            if (primePlus  && f > 1) {
                countOfPrimes++;
                System.out.println(countOfPrimes + ": " + factorialNum + "! + 1 = " + (factorialNum + 1));
            }
            f++;
        }
    }

    private static long getFactorial(long f) {
        long factorial = 1;
        for (long i = 1; i < f; i++) {
            factorial *= i;
        }
        return factorial;
    }

    private static boolean isPrime(long num) {
        if (num < 2) {return false;}
        for (long i = 2; i < num; i++) {
            if (num % i == 0) {return false;}
        }
        return true;
    }
}
Task: Wagstaff primes





Java: import java.math.BigInteger; 

public class Main {
  public static void main(String[] args) {
    BigInteger d = new BigInteger("3"), a;
    int lmt = 25, sl, c = 0;
    for (int i = 3; i < 5808; ) {
      a = BigInteger.ONE.shiftLeft(i).add(BigInteger.ONE).divide(d);
      if (a.isProbablePrime(1)) {
        System.out.printf("%2d %4d ", ++c, i);
        String s = a.toString(); sl = s.length();
        if (sl < lmt) System.out.println(a);
        else System.out.println(s.substring(0, 11) + ".." + s.substring(sl - 11, sl) + " " + sl + " digits");
      }
      i = BigInteger.valueOf(i).nextProbablePrime().intValue();
    }
  }
}
Task: Prime triangle





Java: public class PrimeTriangle {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 2; i <= 20; ++i) {
            int[] a = new int[i];
            for (int j = 0; j < i; ++j)
                a[j] = j + 1;
            if (findRow(a, 0, i))
                printRow(a);                
        }
        System.out.println();
        StringBuilder s = new StringBuilder();
        for (int i = 2; i <= 20; ++i) {
            int[] a = new int[i];
            for (int j = 0; j < i; ++j)
                a[j] = j + 1;
            if (i > 2)
                s.append(" ");
            s.append(countRows(a, 0, i));
        }
        System.out.println(s);
        long finish = System.currentTimeMillis();
        System.out.printf("\nElapsed time: %d milliseconds\n", finish - start);
    }

    private static void printRow(int[] a) {
        for (int i = 0; i < a.length; ++i) {
            if (i != 0)
                System.out.print(" ");
            System.out.printf("%2d", a[i]);
        }
        System.out.println();
    }

    private static boolean findRow(int[] a, int start, int length) {
        if (length == 2)
            return isPrime(a[start] + a[start + 1]);
        for (int i = 1; i + 1 < length; i += 2) {
            if (isPrime(a[start] + a[start + i])) {
                swap(a, start + i, start + 1);
                if (findRow(a, start + 1, length - 1))
                    return true;
                swap(a, start + i, start + 1);
            }
        }
        return false;
    }

    private static int countRows(int[] a, int start, int length) {
        int count = 0;
        if (length == 2) {
            if (isPrime(a[start] + a[start + 1]))
                ++count;
        } else {
            for (int i = 1; i + 1 < length; i += 2) {
                if (isPrime(a[start] + a[start + i])) {
                    swap(a, start + i, start + 1);
                    count += countRows(a, start + 1, length - 1);
                    swap(a, start + i, start + 1);
                }
            }
        }
        return count;
    }

    private static void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    private static boolean isPrime(int n) {
        return ((1L << n) & 0x28208a20a08a28acL) != 0;
    }
}
Task: Erdös-Selfridge categorization of primes





Java: import java.util.*;

public class ErdosSelfridge {
    private int[] primes;
    private int[] category;

    public static void main(String[] args) {
        ErdosSelfridge es = new ErdosSelfridge(1000000);

        System.out.println("First 200 primes:");
        for (var e : es.getPrimesByCategory(200).entrySet()) {
            int category = e.getKey();
            List<Integer> primes = e.getValue();
            System.out.printf("Category %d:\n", category);
            for (int i = 0, n = primes.size(); i != n; ++i)
                System.out.printf("%4d%c", primes.get(i), (i + 1) % 15 == 0 ? '\n' : ' ');
            System.out.printf("\n\n");
        }

        System.out.println("First 1,000,000 primes:");
        for (var e : es.getPrimesByCategory(1000000).entrySet()) {
            int category = e.getKey();
            List<Integer> primes = e.getValue();
            System.out.printf("Category %2d: first = %7d  last = %8d  count = %d\n", category,
                              primes.get(0), primes.get(primes.size() - 1), primes.size());
        }
    }

    private ErdosSelfridge(int limit) {
        PrimeGenerator primeGen = new PrimeGenerator(100000, 200000);
        List<Integer> primeList = new ArrayList<>();
        for (int i = 0; i < limit; ++i)
            primeList.add(primeGen.nextPrime());
        primes = new int[primeList.size()];
        for (int i = 0; i < primes.length; ++i)
            primes[i] = primeList.get(i);
        category = new int[primes.length];
    }

    private Map<Integer, List<Integer>> getPrimesByCategory(int limit) {
        Map<Integer, List<Integer>> result = new TreeMap<>();
        for (int i = 0; i < limit; ++i) {
            var p = result.computeIfAbsent(getCategory(i), k -> new ArrayList<Integer>());
            p.add(primes[i]);
        }
        return result;
    }

    private int getCategory(int index) {
        if (category[index] != 0)
            return category[index];
        int maxCategory = 0;
        int n = primes[index] + 1;
        for (int i = 0; n > 1; ++i) {
            int p = primes[i];
            if (p * p > n)
                break;
            int count = 0;
            for (; n % p == 0; ++count)
                n /= p;
            if (count != 0) {
                int category = (p <= 3) ? 1 : 1 + getCategory(i);
                maxCategory = Math.max(maxCategory, category);
            }
        }
        if (n > 1) {
            int category = (n <= 3) ? 1 : 1 + getCategory(getIndex(n));
            maxCategory = Math.max(maxCategory, category);
        }
        category[index] = maxCategory;
        return maxCategory;
    }

    private int getIndex(int prime) {
       return Arrays.binarySearch(primes, prime);
    }
}
Task: Earliest difference between prime gaps





Java: import java.util.HashMap;
import java.util.Map;

public class PrimeGaps {
    private Map<Integer, Integer> gapStarts = new HashMap<>();
    private int lastPrime;
    private PrimeGenerator primeGenerator = new PrimeGenerator(1000, 500000);

    public static void main(String[] args) {
        final int limit = 100000000;
        PrimeGaps pg = new PrimeGaps();
        for (int pm = 10, gap1 = 2;;) {
            int start1 = pg.findGapStart(gap1);
            int gap2 = gap1 + 2;
            int start2 = pg.findGapStart(gap2);
            int diff = start2 > start1 ? start2 - start1 : start1 - start2;
            if (diff > pm) {
                System.out.printf(
                    "Earliest difference > %,d between adjacent prime gap starting primes:\n"
                    + "Gap %,d starts at %,d, gap %,d starts at %,d, difference is %,d.\n\n",
                    pm, gap1, start1, gap2, start2, diff);
                if (pm == limit)
                    break;
                pm *= 10;
            } else {
                gap1 = gap2;
            }
        }
    }

    private int findGapStart(int gap) {
        Integer start = gapStarts.get(gap);
        if (start != null)
            return start;
        for (;;) {
            int prev = lastPrime;
            lastPrime = primeGenerator.nextPrime();
            int diff = lastPrime - prev;
            gapStarts.putIfAbsent(diff, prev);
            if (diff == gap)
                return prev;
        }
    }
}
Task: Colorful numbers





Java: public class ColorfulNumbers {
    private int count[] = new int[8];
    private boolean used[] = new boolean[10];
    private int largest = 0;

    public static void main(String[] args) {
        System.out.printf("Colorful numbers less than 100:\n");
        for (int n = 0, count = 0; n < 100; ++n) {
            if (isColorful(n))
                System.out.printf("%2d%c", n, ++count % 10 == 0 ? '\n' : ' ');
        }

        ColorfulNumbers c = new ColorfulNumbers();

        System.out.printf("\n\nLargest colorful number: %,d\n", c.largest);

        System.out.printf("\nCount of colorful numbers by number of digits:\n");
        int total = 0;
        for (int d = 0; d < 8; ++d) {
            System.out.printf("%d   %,d\n", d + 1, c.count[d]);
            total += c.count[d];
        }
        System.out.printf("\nTotal: %,d\n", total);
    }

    private ColorfulNumbers() {
        countColorful(0, 0, 0);
    }

    public static boolean isColorful(int n) {
        
        if (n < 0 || n > 98765432)
            return false;
        int digit_count[] = new int[10];
        int digits[] = new int[8];
        int num_digits = 0;
        for (int m = n; m > 0; m /= 10) {
            int d = m % 10;
            if (n > 9 && (d == 0 || d == 1))
                return false;
            if (++digit_count[d] > 1)
                return false;
            digits[num_digits++] = d;
        }
        
        int products[] = new int[36];
        for (int i = 0, product_count = 0; i < num_digits; ++i) {
            for (int j = i, p = 1; j < num_digits; ++j) {
                p *= digits[j];
                for (int k = 0; k < product_count; ++k) {
                    if (products[k] == p)
                        return false;
                }
                products[product_count++] = p;
            }
        }
        return true;
    }

    private void countColorful(int taken, int n, int digits) {
        if (taken == 0) {
            for (int d = 0; d < 10; ++d) {
                used[d] = true;
                countColorful(d < 2 ? 9 : 1, d, 1);
                used[d] = false;
            }
        } else {
            if (isColorful(n)) {
                ++count[digits - 1];
                if (n > largest)
                    largest = n;
            }
            if (taken < 9) {
                for (int d = 2; d < 10; ++d) {
                    if (!used[d]) {
                        used[d] = true;
                        countColorful(taken + 1, n * 10 + d, digits + 1);
                        used[d] = false;
                    }
                }
            }
        }
    }
}
Task: Rhonda numbers





Java: public class RhondaNumbers {
    public static void main(String[] args) {
        final int limit = 15;
        for (int base = 2; base <= 36; ++base) {
            if (isPrime(base))
                continue;
            System.out.printf("First %d Rhonda numbers to base %d:\n", limit, base);
            int numbers[] = new int[limit];
            for (int n = 1, count = 0; count < limit; ++n) {
                if (isRhonda(base, n))
                    numbers[count++] = n;
            }
            System.out.printf("In base 10:");
            for (int i = 0; i < limit; ++i)
                System.out.printf(" %d", numbers[i]);
            System.out.printf("\nIn base %d:", base);
            for (int i = 0; i < limit; ++i)
                System.out.printf(" %s", Integer.toString(numbers[i], base));
            System.out.printf("\n\n");
        }
    }
    
    private static int digitProduct(int base, int n) {
        int product = 1;
        for (; n != 0; n /= base)
            product *= n % base;
        return product;
    }
     
    private static int primeFactorSum(int n) {
        int sum = 0;
        for (; (n & 1) == 0; n >>= 1)
            sum += 2;
        for (int p = 3; p * p <= n; p += 2)
            for (; n % p == 0; n /= p)
                sum += p;
        if (n > 1)
            sum += n;
        return sum;
    }
     
    private static boolean isPrime(int n) {
        if (n < 2)
            return false;
        if (n % 2 == 0)
            return n == 2;
        if (n % 3 == 0)
            return n == 3;
        for (int p = 5; p * p <= n; p += 4) {
            if (n % p == 0)
                return false;
            p += 2;
            if (n % p == 0)
                return false;
        }
        return true;
    }
     
    private static boolean isRhonda(int base, int n) {
        return digitProduct(base, n) == base * primeFactorSum(n);
    }
}
Task: Pan base non-primes





Java: public class PanBaseNonPrimes {
    public static void main(String[] args) {
        System.out.printf("First 50 prime pan-base composites:\n");
        int count = 0;
        for (long n = 2; count < 50; ++n) {
            if (isPanBaseNonPrime(n)) {
                ++count;
                System.out.printf("%3d%c", n, count % 10 == 0 ? '\n' : ' ');
            }
        }
    
        System.out.printf("\nFirst 20 odd prime pan-base composites:\n");
        count = 0;
        for (long n = 3; count < 20; n += 2) {
            if (isPanBaseNonPrime(n)) {
                ++count;
                System.out.printf("%3d%c", n, count % 10 == 0 ? '\n' : ' ');
            }
        }
    
        final long limit = 10000;
        count = 0;
        int odd = 0;
        for (long n = 2; n <= limit; ++n) {
            if (isPanBaseNonPrime(n)) {
                ++count;
                if (n % 2 == 1)
                    ++odd;
            }
        }
    
        System.out.printf("\nCount of pan-base composites up to and including %d: %d\n",
                            limit, count);
        double percent = 100.0 * odd / count;
        System.out.printf("Percent odd  up to and including %d: %f\n",
                            limit, percent);
        System.out.printf("Percent even up to and including %d: %f\n",
                            limit, 100.0 - percent);
    }

    private static boolean isPanBaseNonPrime(long n) {
        if (n < 10)
            return !isPrime(n);
        if (n > 10 && n % 10 == 0)
            return true;
        byte[] d = new byte[20];
        int count = digits(n, d);
        byte max_digit = 0;
        for (int i = 0; i < count; ++i) {
            if (max_digit < d[i])
                max_digit = d[i];
        }
        for (long base = max_digit + 1; base <= n; ++base) {
            if (isPrime(fromDigits(d, count, base)))
                return false;
        }
        return true;
    }

    private static final long[] WHEEL = {4, 2, 4, 2, 4, 6, 2, 6};

    private static boolean isPrime(long n) {
        if (n < 2)
            return false;
        if (n % 2 == 0)
            return n == 2;
        if (n % 3 == 0)
            return n == 3;
        if (n % 5 == 0)
            return n == 5;
        for (long p = 7;;) {
            for (int i = 0; i < 8; ++i) {
                if (p * p > n)
                    return true;
                if (n % p == 0)
                    return false;
                p += WHEEL[i];
            }
        }
    }

    
    private static int digits(long n, byte[] d) {
        int count = 0;
        for (; n > 0 && count < d.length; n /= 10, ++count)
            d[count] = (byte)(n % 10);
        return count;
    }

    
    private static long fromDigits(byte[] a, int count, long base) {
        long n = 0;
        while (count-- > 0)
            n = n * base + a[count];
        return n;
    }
}
Task: CalmoSoft primes





Java: import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CalmoSoftPrimes {
    public static void main(String[] args) {
        PrimeGenerator primeGen = new PrimeGenerator(100000, 250000);
        List<Integer> primes = new ArrayList<>();
        final int[] limits = {100, 5000, 10000, 500000, 50000000};
        long total = 0;
        int last = 0;
        int prime = primeGen.nextPrime();
        for (int limit : limits) {
            do {
                primes.add(prime);
                total += prime;
                ++last;
                prime = primeGen.nextPrime();
            } while (prime < limit);
            long sum = total;
            int longest = 1;
            List<Integer> starts = new ArrayList<>();
            for (int start = 0; start <= last - longest; ++start) {
                long s = sum;
                for (int finish = last; finish-- >= start + longest;) {
                    if (isPrime(s)) {
                        int length = finish - start + 1;
                        if (length > longest) {
                            longest = length;
                            starts.clear();
                        }
                        starts.add(start);
                    }
                    s -= primes.get(finish);
                }
                sum -= primes.get(start);
            }
            System.out.printf("For primes up to %d:\nThe following sequence%s of %d consecutive primes yield%s a prime sum:\n",
                              limit, starts.size() == 1 ? "" : "s", longest, starts.size() == 1 ? "s" : "");
            for (int i = 0; i < starts.size(); ++i) {
                int start = starts.get(i);
                sum = 0;
                for (int j = start; j < start + longest; ++j)
                    sum += primes.get(j);
                final String separator = " + ";
                if (longest > 12) {
                    System.out.print(join(separator, primes.subList(start, start + 6))
                        + separator + "..." + separator
                        + join(separator, primes.subList(start + longest - 6, start + longest)));
                } else {
                    System.out.print(join(separator, primes.subList(start, start + longest)));
                }
                System.out.println(" = " + sum);
            }
            System.out.println();
        }
    }

    private static <T> String join(String separator, List<T> list) {
        return list.stream().map(Object::toString).collect(Collectors.joining(separator));
    }

    private static boolean isPrime(long n) {
        if (n < 2)
            return false;
        if (n % 2 == 0)
            return n == 2;
        if (n % 3 == 0)
            return n == 3;
        for (long p = 5; p * p <= n; p += 4) {
            if (n % p == 0)
                return false;
            p += 2;
            if (n % p == 0)
                return false;
        }
        return true;
    }
}
Task: Singular value decomposition





Java: import Jama.Matrix;
public class SingularValueDecomposition {
    public static void main(String[] args) {
        double[][] matrixArray = {{3, 0}, {4, 5}};
        var matrix = new Matrix(matrixArray);
        var svd = matrix.svd();
        svd.getU().print(0, 10); 
        svd.getS().print(0, 10);
        svd.getV().print(0, 10);
    }
}
Task: WiktionaryDumps to words





Java: import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class MyHandler extends DefaultHandler {
    private static final String TITLE = "title";
    private static final String TEXT = "text";

    private String lastTag = "";
    private String title = "";

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String regex = ".*==French==.*";
        Pattern pat = Pattern.compile(regex, Pattern.DOTALL);

        switch (lastTag) {
            case TITLE:
                title = new String(ch, start, length);
                break;
            case TEXT:
                String text = new String(ch, start, length);
                Matcher mat = pat.matcher(text);
                if (mat.matches()) {
                    System.out.println(title);
                }
                break;
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        lastTag = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        lastTag = "";
    }
}

public class WiktoWords {
    public static void main(java.lang.String[] args) {
        try {
            SAXParserFactory spFactory = SAXParserFactory.newInstance();
            SAXParser saxParser = spFactory.newSAXParser();
            MyHandler handler = new MyHandler();
            saxParser.parse(new InputSource(System.in), handler);
        } catch(Exception e) {
            System.exit(1);
        }
    }
}
Task: B-spline





Java: 

int abscissae[]={171,185,202,202,328,208,241,164,69,139,72,168};
int ordinates[] = {171,111,109,189 ,160,254 ,330 ,252,278 ,208 ,148 ,172};

void setup() {
  size(450, 450);
  background(255);
  smooth();

  noFill();
  stroke(0);
  
  beginShape();
  for(int i=0;i<abscissae.length;i++){
    curveVertex(abscissae[i], ordinates[i]);
  }
  endShape();

  fill(255, 0, 0);
  noStroke();
  for (int i = 0; i < abscissae.length; i ++) {
    ellipse(abscissae[i], ordinates[i], 3, 3);
  }
}