JavaでCollatz conjectureの実装
https://en.wikipedia.org/wiki/Collatz_conjecture
の実装をしていて、コマンドラインでargs0,args1が与えられます。
複数のスレッドによる作業で実行時間が小さくなることを確認したいです。
メソッドcheckがCollatz conjectureの計算部分です。
##エラー部分
CheckCollatzMT.java:62: error: Nthreads is not abstract and does not override abstract method run() in Runnable
class Nthreads implements Runnable {
^
CheckCollatzMT.java:64: error: cannot find symbol
boolean v = CheckCollatz.check(new BigInteger(args[0]));
^
symbol: variable args
location: class Nthreads
###ソースコード```java
import java.math.BigInteger;
import java.util.Date;
import java.io.*;
public class CheckCollatzMT {
protected static BigInteger two = new BigInteger("2");
protected static BigInteger three = new BigInteger("3");
protected static boolean v;
public static boolean check(BigInteger n) {
//System.out.println(n);
if(n.equals(java.math.BigInteger.ONE)){ //n==1
return true;
}
else if(n.mod(two).equals(java.math.BigInteger.ZERO)){
n = n.divide(two);
return check(n);
}
else{
n = n.multiply(three).add(java.math.BigInteger.ONE);
return check(n);
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage:");
System.out.println("java CheckCollatzMT n num_threads");
System.out.println("where positive integers i: 1<= i <= n will be checked");
System.out.println("and num_threads is the number of threads to use");
System.exit(1);
}
long start_time = new Date().getTime(); if(args[1] == "1"){ boolean v = CheckCollatz.check(new BigInteger(args[0])); } else { v = CheckCollatz.check(new BigInteger(args[0])); Nthreads sample = new Nthreads(); Thread thread = new Thread(sample); for(int i=0; i<Integer.valueOf(args[1]); i++){ thread.start(); } } long end_time = new Date().getTime(); System.out.println("Ellapsed time: " + (end_time-start_time) + "ms"); if(v){ System.out.println("The conjecture seems valid up to n="+args[0]); } else { System.out.println("The conjecture is not valid"); }
}
}
class Nthreads implements Runnable {
public void run(BigInteger n) {
boolean v = CheckCollatz.check(new BigInteger(args[0]));
}
}
####試したこと Nthreadsクラスをインナークラスにしたりした。
回答1件
あなたの回答
tips
プレビュー