简体   繁体   English

JavaScript 用于 Project Euler 问题

[英]JavaScript for Project Euler problems

I am trying to solve Q10 from Project Euler.我正在尝试从 Project Euler 解决Q10 I am using JavaScript and Sieve of Atkins algorithm to solve the problem.我正在使用 JavaScript 和Sieve of Atkins算法来解决问题。 When I run the code on browsers(Safari and FF) the browsers prompts that the script is unresponsive.当我在浏览器(Safari 和 FF)上运行代码时,浏览器会提示脚本无响应。 Even if I let the script to continue I never got the answer.即使我让脚本继续,我也没有得到答案。 I know there are threads for the same Project Euler problem.我知道有相同的 Project Euler 问题的线程。

My questions would be:-我的问题是:-

1.How far JavaScript is capable to solve such complex mathematical problems for browsers? 1.JavaScript对于浏览器能解决如此复杂的数学问题有多远?

2.Is there any other environment where I can I test my JavaScript programs? 2.是否有其他环境可以测试我的 JavaScript 程序?

Thank you All.谢谢你们。

The goal of project Euler is to get you thinking, mathematically. Euler 项目的目标是让你在数学上思考。 Think of brute forcing, and you will be stuck.想想蛮力,你会被卡住。 Here is an implementation of the Sieve of Eratosthenes这是埃拉托色尼筛的一个实现


function problem10() {

    var i, j, k, l = Math.floor((2000000-1)/2), a = [];
    for (i = 0; i < l; i++) {
        a[i] = true;
    } var m = Math.sqrt(2000000);
    for (i = 0; i <= m; i++) {
        if (a[i]) {
            j = 2 * i + 3;
            k = i + j;
            while (k < l) {
                a[k] = false;
                k += j;
            }
        }
    } var s = 2;
    for (i = 0; i < l; i++) {
        if (a[i]) {
            s += 2 * i + 3;
        }
    }
    return s;

}

var d1 = new Date().getTime();
var answer = problem10();
var d2 = new Date().getTime();

console.log('Answer:' + answer + ' time:' + (d2 - d1));

You can run it on the chrome developer's console (Ctrl + Shift + J).您可以在 chrome 开发人员的控制台(Ctrl + Shift + J)上运行它。 And guess what, it clocks 0.1 second.你猜怎么着,它的时钟是 0.1 秒。

  1. I would have thought as capable as any other - JavaScript implementations have been optimised a lot in recent years thanks to increased used in the web.我会认为 JavaScript 实现在最近几年得到了很多优化,这要归功于 web 中使用的增加。

  2. You can use either node.js or CScript (a command line version of the Windows Script host - this is supplied as part of Windows).您可以使用node.jsCScript (Windows 脚本宿主的命令行版本 - 作为 Windows 的一部分提供)。

If I remember my implementation of that question (in Python) was significantly slower than I thought it would be.如果我记得我对那个问题的实现(在 Python 中)比我想象的要慢得多。 The chances are the slowness is due to your algorithm rather than the language.缓慢的可能性是由于您的算法而不是语言。

function problem10(){

    var a = 0;

    function isPrime(n){
        var i = 2;
        var b = true;
        while(i<=Math.sqrt(n) && b){
            b = n%i===0?false:true;
            i++;
        }
        return n<2?false:b;
    }

    for(i=0;i<2000000;i++){
        if(isPrime(i)){
            a+=i;
        }
    }
    return a;

}

You could try testing your implementation on node.js .您可以尝试在node.js上测试您的实现。

However, I would bet that you have a problem with your code.但是,我敢打赌,您的代码有问题。 JavaScript in a modern browser is pretty quick (and generally you should get Project Euler answers very quickly; it's not designed to require high amounts of computing power).现代浏览器中的 JavaScript 非常快(通常您应该很快得到 Project Euler 的答案;它的设计目的不是需要大量的计算能力)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM