简体   繁体   中英

Problem while trying to find prime numbers between 2 and 100

I am having trouble finding the problem with my short program, meant to find all the prime numbers between 2 and 100. I have tried rewriting the program several times; but my browser always slows to a halt when I try to run it. Any help would be much appreciated!

        function is_prime(x)
        {
            for (i = 2; i <= x / 2; ++i)
                if (x % i == 0) return false;
            return true;
        }

        for (i = 2; i < 100; ++i)
            if (is_prime(i)) console.log(i);

The i in your loops are not declared

 function is_prime(x) { for (let i = 2; i <= x / 2; ++i) if (x % i == 0) return false; return true; } for (let i = 2; i < 100; ++i) if (is_prime(i)) console.log(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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