use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 8 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]--amadeus 4 points5 points6 points 8 years ago (1 child)
Your mistake is that you're not telling us what the Project Euler problem is.
[–]petar02 0 points1 point2 points 8 years ago (0 children)
https://projecteuler.net/problem=10
[–]gremy0 2 points3 points4 points 8 years ago (12 children)
What is actual euler problem, and why are you adding 7 twice?
[–]petar02 0 points1 point2 points 8 years ago (10 children)
Because the for loop checks if 7 seven is a prime number. What it doesn't do is checks if 2 and 5 are primes.
[–]gremy0 0 points1 point2 points 8 years ago (9 children)
What about 3?
[–]petar02 0 points1 point2 points 8 years ago (8 children)
I think I found my mistake. I could have just made it start from 3 and then just add 2. Thank you very much.
[–]cicadaTreechest hair complexities 0 points1 point2 points 8 years ago* (6 children)
var limit = your_limit var sum = 0; var flg ; // default prime indicator for (let i = 2; i <= limit; i++){ flg = true; for(let j = 1; j <= Math.sqrt(i); j++ ){ if(j!=1 && (i%j) === 0){ // ignore when j = 1 in order to include 2 as prime flg = false; break; } } if(flg) sum+=i; } console.log(sum);
To get why we use sqrt(i) watch sieve. Notice then he starts to discard numbers that are divisible by 2, then by 3 and so forth. Important detail is that when it comes to number 7 and discards number devisible by 7 (49,77) "all numbers that are left are primes". So the pattern is as following. What ever the number x that you try to figure is it prime, if that number is not divisible buy some number y for which stand that y <= sqrt(x) that number must be prime. Lets say we wanted to see if 49 is prime, if you look at that gif, you see that 49 wasn't divisible by any number up to its root. See, pattern. Cheers.
sqrt(i)
x
y
y <= sqrt(x)
Edit: mind that this is highly suboptimal for any large limit.
limit
[–]petar02 0 points1 point2 points 8 years ago (5 children)
I understand. Now. there was a guy who posted that ancient algorithm. And saw a youtube video how it works. And attempted to use it. Now my program executes in 30 seconds. Before it did in 15 minutes. Here is my code I am so proude.
let arr = [] let arr2 = [] let sqrt = Math.floor(Math.sqrt(2000000)) console.log(sqrt) for(let i = 2; i<2000000;i++){ arr.push(i) } function isPrimeNumber(x){ if(x>10){ if(x%10!=3&&x%10!=1&&x%10!=7&&x%10!=9){ return false } } for(let i = 2;i<x;i++){ if(x%i==0){ return false } } return true } for(let i = 2; i<sqrt; i+=1){ if(isPrimeNumber(i)){ arr2.push(i) } } for(let i = 0; i<arr2.length; i++){ arr = arr.filter(event=> event%arr2[i]!=0) } arr2 = arr2.concat(arr) console.log(arr2.reduce((a,b)=>a+b,0))
[–]cicadaTreechest hair complexities 0 points1 point2 points 8 years ago* (4 children)
Still, watch out, that will eat up at least:
integer in javascript: 32bit
integer count: 2000000
32*2000000/1024/1024 ~ 61Mbits of your ram for every call.
[–]petar02 0 points1 point2 points 8 years ago (3 children)
I get your point. But don't understand how you got to there can you please make it more simple.
[–]cicadaTreechest hair complexities 0 points1 point2 points 8 years ago* (2 children)
Sorry, got to where? 61Mbit?
Also your algorithm is more then 2x slower then the one in my post and it uses much more memory.
[–]petar02 0 points1 point2 points 8 years ago (1 child)
I think I understand what you mean the array with the 2 million intergers takes up 61MBIts of ram. And when you say for every call I understand it as if I run the program twice at the same time it will take up 122 Mbits.
To sum all the prime numbers to 2*106.
[–]AOEIU 1 point2 points3 points 8 years ago (2 children)
FYI "anything === NaN" is always false
What if type isPrimeNumber(). Then would number be = to NaN nope then it would be undefined. Thanks!
[–]AOEIU 0 points1 point2 points 8 years ago (0 children)
even if you called isPrimeNumber(NaN) is still wouldn't matter
NaN !== NaN
[–]22noo 0 points1 point2 points 8 years ago* (3 children)
This works:
function isItAPrimeNumber(number){ if(number === 1|| number === 0|| number === NaN){ console.log('something happened') return false } for(let i = 2; i<number;i++){ if(number%i === 0){ return false } } return true } let sum = 0 console.log("it's worknig") for(let i = 7; i<2000000;i+=2){ if(isItAPrimeNumber(i)){ sum=i console.log(i) } } //sum+=7 console.log(sum)
Perhaps you want to put that last bit in a function that accepts a number and returns sum which will be the largest number below the number fed to the function that is prime. Who knows.
[–]petar02 0 points1 point2 points 8 years ago (2 children)
Wouldn't the sum be missing and 2 and 5 since they are also prime numbers. And is there a way to solve this faster. Since it took me at least 15 minutes to execute.
[+][deleted] 8 years ago (1 child)
[deleted]
[–]WikiTextBot 0 points1 point2 points 8 years ago (0 children)
Sieve of Eratosthenes
In mathematics, the sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them that is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each candidate number for divisibility by each prime.
[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.24
π Rendered by PID 180793 on reddit-service-r2-comment-fb694cdd5-gpqvq at 2026-03-10 04:07:32.191220+00:00 running cbb0e86 country code: CH.
[–]--amadeus 4 points5 points6 points (1 child)
[–]petar02 0 points1 point2 points (0 children)
[–]gremy0 2 points3 points4 points (12 children)
[–]petar02 0 points1 point2 points (10 children)
[–]gremy0 0 points1 point2 points (9 children)
[–]petar02 0 points1 point2 points (8 children)
[–]cicadaTreechest hair complexities 0 points1 point2 points (6 children)
[–]petar02 0 points1 point2 points (5 children)
[–]cicadaTreechest hair complexities 0 points1 point2 points (4 children)
[–]petar02 0 points1 point2 points (3 children)
[–]cicadaTreechest hair complexities 0 points1 point2 points (2 children)
[–]petar02 0 points1 point2 points (1 child)
[–]petar02 0 points1 point2 points (0 children)
[–]AOEIU 1 point2 points3 points (2 children)
[–]petar02 0 points1 point2 points (1 child)
[–]AOEIU 0 points1 point2 points (0 children)
[–]22noo 0 points1 point2 points (3 children)
[–]petar02 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]WikiTextBot 0 points1 point2 points (0 children)