所以Scala应该和Java一样快.我正在重新审视我最初用Java解决的Scala中的一些Project Euler问题.具体问题5:"从1到20的所有数字均可被整除的最小正数是多少?"
这是我的Java解决方案,在我的机器上完成需要0.7秒:
public class P005_evenly_divisible implements Runnable{
final int t = 20;
public void run() {
int i = 10;
while(!isEvenlyDivisible(i, t)){
i += 2;
}
System.out.println(i);
}
boolean isEvenlyDivisible(int a, int b){
for (int i = 2; i <= b; i++) {
if (a % i != 0)
return false;
}
return true;
}
public static void main(String[] args) {
new P005_evenly_divisible().run();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我对Scala的"直接翻译",需要103秒(147倍!)
object P005_JavaStyle {
val t:Int = 20;
def run {
var …
Run Code Online (Sandbox Code Playgroud)