[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

>asked to sum all prime numbers under two million

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 146
Thread images: 16

File: IMG_0112.jpg (36KB, 418x438px) Image search: [Google]
IMG_0112.jpg
36KB, 418x438px
>asked to sum all prime numbers under two million
>>
>>59698274
yeah that's easy lmao
what are you some sort of dumbass
>>
int total = 0;
for(int x = 0; x < 1000000; ++x){
for(int y = 0; y < 1000000; ++y){
if(x % y == 0){
for(int z = 0; z < x; ++z){
++total;
}
}
}
}
>>
>>59698352
Now do it in QBASIC
>>
>>59698309
I wrote. 20,000 line divine intellect compiler that operates just in time and ahead of time. You seem to be in denial, why don't you download my two meg distribution and you can use my fuckin compiler. You're a nigger, you're a fuckin nigger.
>>
>>59698352
this is identical to javascript in almost every way.
Just saying :)
>>
>>59698352
Why did you do that fugly loop with z? Why not just add x to the total?
>>
>>59698274
>In less than a second.

It's real easy.
>>
SIEEEEEEEEEEEEEEEEEEEEEEEVE
>>
>>59698424
>an incredibly basic function will look pretty much the same no natter what language or syntax is used
rally naek mi thnk
>>
>>59698352
i am mentally challenge. can you explain your code?

pls
>>
>>59698274
unless they allow you to look up methods to check for primes, what can they legitimately expect from you other than checking whether the number is even and larger than 2, and then running a loop that checks the modulus of all the numbers up to half the the original number. it will be slow as shit, but it is guaranteed to work.
>>
>>59698274
Use a sieve.
>>
>up to half the the original number.

You only need to check up to the square root. For every divisor less than the square root there's a corresponding one above it. If a number doesn't have a divisor less than or equal to its square root it's not gonna have any above it.
>>
package main

import (
"fmt"
"primes"
)

func main() {
result := 0
seq := primes.NewSequence(2)
for i, p := range seq.Next() {
if(i == 2000000) {
break
}

result += p
}
fmt.Println(result)
}
>>
#include <stdio.h>
#include <math.h>

#define SIZE 2000000

void sieve(char *primes, int size){
int sqr = (int) sqrt(size);
for(int i = 2;i<=sqr;i++){
if(!primes[i])
for(int j = 2;i*j<=size;j++){
primes[j*i]=1;
}
}
}

int main(){
char primes[SIZE] = {0};
sieve(primes,SIZE-1);
long sum = 0;
for(int i = 2;i<SIZE;i++){
if(!primes[i]) sum+=i;
}
printf("%ld\n",sum);
return 0;
}
>>
File: 40202657.png (6KB, 210x229px) Image search: [Google]
40202657.png
6KB, 210x229px
>>59698274
>google "list of prime numbers"
>copy paste the ones below 2 million
>declare an array with those primes in your program
>it only has to sum every part of the array in one cycle and return the value
fast as fuck
>>
>>59698309
#!/usr/bin/python3

import math
n = 4*(10**6)
primes = []

def get_primes(n):
sieve = [False] * n
primenumbers = []
i = 2

for i in range(2, n):
if not sieve[i]:
primenumbers.append(i)

for j in range(i*i, n, i):
sieve[j] = True
return primenumbers

primes = get_primes(n)
sum = 0
for prime in primes:
sum += prime

print(sum)


544501644261
>>
>>59700072
woops. That was the wrong limit. The correct sum is: 142913828922
>>
What's the point of your thread, ausist?
>>
>>59700032
>copy paste
>not making your program search the web and download the data-set
What are you a fucking monkey
>>
>>59698748
This segfaults.

gcc -o fuckpython fuckpython.c -L/usr/include/math.h -lm


segmentation fault
>>
>>59700072
>using import math
>>
>>59698352
>++total
What does that line do?
Shouldn't it be something like
total+= z?
>>
>>59700631
I compiled it and I don't get segmentation fault.
>>
>>59700813
How did you compile it?
>>
File: 1487630677075.png (199KB, 1800x1578px) Image search: [Google]
1487630677075.png
199KB, 1800x1578px
>>59700092
>>
>>59698274
Use MATLAB.
>>
>>59698352
This is retarded

X and y are always the same

So x%y ==0 is always true

But z is always =x
>>
#include <prime.h>

int main() {
uint primesum;
for (int i = 0; i < 2000000; i++)
{
if (checkPrime(i))
primesum += i;
}


ez
>>
>>59701109
is this matlab?
>>
>>59698274
 
long sum;
for(int i = 0; i <=2000000; i++){
if((i%2)!=0){
sum+=i;
}
}

Done
>>
>>59701168
syntax aside. This checks out.
>>
>>59701168
This is expensive.
>>
>>59701186
Explain
>>
>>59701186
2 million divisions by 2. On cpu's that have division on chip as an instruction.

Would love to see the benchmark. Guessing ms
>not exactly expensive

Would like to see your better approach.
> I'm not >>59701168 btw
>>
>>59698352
comyydy gold lad
>>
>>59701186
maybe 30 years ago when CPUs weren't preloaded with division and would do it by addition yeah
>>
type ('state, 'a) stream =
{
state : 'state;
generate : 'state -> 'state * 'a;
}
;;

let mk_stream state generate =
{
state = state;
generate = generate;
}
;;

let filter p stream =
let state = stream.state in
let rec generate state =
let state, n = stream.generate state in
if n mod p = 0 then
generate state
else
state, n in
mk_stream state generate
;;

let sieve stream =
let state = stream in
let generate stream =
let state, p = stream.generate stream.state in
let stream = { stream with state = state; } in
let stream = filter p stream in
stream, p in
mk_stream state generate
;;

let numbers =
let state = 2 in
let generate state = succ state, state in
mk_stream state generate
;;

let primes = sieve numbers;;

let next stream =
let state, x = stream.generate stream.state in
mk_stream state stream.generate, x
;;

let main () =
let rec loop sum stream =
let stream, p = next stream in
if p < 2_000_000 then
loop (sum + p) stream
else
sum in
let sum = loop 0 primes in
print_endline (string_of_int sum)
;;

let () = main ();;


result

~$ time ./prime
142913828922

real 7m5.209s
user 7m5.147s
sys 0m0.027s
>>
>>59698274
r8 and h8

https://github.com/alex47/PrimSumCalc
>>
int sum = 0;
for (int cand = 0; cand <= 2_000_000; cand++) {
boolean prime = true;
for (int div = 2; div < cand; div++) {
if (cand % div == 0) {
prime = false;
break;
}
}
if (prime) {
sum += cand;
}
}
System.out.println("Sum: " + sum);
>>
>>59698575
Yep that will use 2Mb of memory and is actually pretty fast.
>>
>>59701168
Fuck I did odd not prime but whatever I'm going to bed
>>
>>59701168
>>59701184
>this checks out
my man it does not
>>
>>59698352
Exception in thread "main" java.lang.ArithmeticException: / by zero
>>
>>59701168
WTF is this, do you even do math?
>>
>>59701432
>>59701443

This
>>59701419
>>
>>59700879
>not understanding how for loops work
>>
>>59701168
>every odd number is a prime
>>59698352
kek
>>59701401
terrible. also what is overflow?
>>
>>59701378
What the fuck? That's slow as fuck.

For mine ( >>59700072 ) I get:
time ./primes.py 
142913828922

real 0m0.464s
user 0m0.448s
sys 0m0.012s
>>
>>59701570
>What the fuck? That's slow as fuck.
Poorly coded.
>>
File: IMG_1091.jpg (573KB, 3000x2000px) Image search: [Google]
IMG_1091.jpg
573KB, 3000x2000px
But how long does it take to calculate the sum 9,6Mhz and only 1k flash?

Gotta do some sport first though. BUT WE WILL GET THERE
>>
>>59698274
You could generate primes with a true sieve of Erathostenes, then just sum them.

(For huge numbers there are of course better algorithms than the Sieve of Erathostenes - how much does this need to scale?)
import scala.annotation.tailrec
import scala.collection.parallel.mutable

def sieveOfEratosthenes(limit: Int) = {
val (primes: mutable.ParSet[Int], sqrtLimit) = (mutable.ParSet.empty ++ (2 to limit), math.sqrt(limit).toInt)
@tailrec
def prim(candidate: Int): Unit = {
if (candidate <= sqrtLimit) {
if (primes contains candidate) primes --= candidate * candidate to limit by candidate
prim(candidate + 1)
}
}
prim(2)
primes
}

sieveOfEratosthenes(2000000).sum
>>
>>59700631
-L is for adding to the library search path, -I is for adding to the header search path.

In a sane installation,
gcc -o fuckpython fuckpython.c -lm

should work just fine.
>>
File: qt.png (209KB, 1422x1958px) Image search: [Google]
qt.png
209KB, 1422x1958px
can anyone beat my time?

I'm using >>59701394
>>
>>59701680
How much RAM? I doubt you can hold all the 2 million bits (250 kB in packed form) for a sieve.
All you need for testing every odd number larger than 2 somewhat fast is storage for all primes under floor(sqrt(2000000))=1414, which comes out to 446 bytes in 16-bit integers.
>>
haskell
 
solution = sum $ takeWhile (<2000000) primes
where primes = 2:[x|x<-[3,5..], and [mod x y /=0 | y<-takeWhile (\y->y^2<=x) primes]]

real 12.293s
user 12.180s
>>
File: asd.png (49KB, 510x307px) Image search: [Google]
asd.png
49KB, 510x307px
>>59702161
Yeah the sieve does not work. Maybe if I sum them prime by prime. Walking through without a sieve.
But too lazy right now.
>>
Lel, functionalets
>>59698748's sieve code runs in 120 ms (compiled statically, because the dynamic loader can add about 0.2 seconds with a shitty SD card) on an original Raspberry Pi with default 700 MHz clock (change "sum" to long long resp. "%lld" in output), in about 10 ms on a cheap-ass 3.1 GHz VPS.
>>
>>59702205
That's what I meant. But instead of testing every number x by dividing by every possible prime factor (3, 5, 7, 9, …) smaller than sqrt(x), you're keeping a list of all primes smaller than sqrt(N) that you have found, which should accelerate things once you test integers larger than ~1000.

That still doesn't work out well if you have less than 512 byte of RAM (that spec sheet only talks about size of Program Flash and Data EEPROM) or only a 8-bit microcontroller.
>>
>>59702024
I don't know why but it still segfaults.
>>
Is there any clever way to figure out if a number is prime?
>>
>>59702579
literally no
>>
>>59698352
>==0
Kys
>>
>>59702579
You can win a prize if you find the next highest prime number. Good luck. People actually attempt this.
>>
>>59701119
numbers = 1:1999999;
primesIndices = isprime(numbers);
primes = numbers(primeIndices);
primeSum= sum(primes);
>>
>>59702579
https://en.wikipedia.org/wiki/Primality_test#Fast_deterministic_tests

Yes, but some of them rely on theorems that haven't been proven yet, also their computational effort (a lot of code and a lot of possible bugs) isn't worth it over the naive approaches for such relatively small numbers.
>>
>>59698352
>average java programmer
>>
long SUM = 0;

for (int i = 0; i <= 2_000_000; i++) {
if (KNOWN_PRIMES.getOrDefault(i, Boolean.FALSE)) {
SUM += i;
}
}


Result: 142913828922

takes ~120ms on my low end laptop
>>
Moved up from a attiny13 to atmega8a.

calculated the sum of primes below 200000.
2 million would have caused an overflow as I figured.
This took a few minutes though, because I haven't implemented the sieve.
Will now try to fix the overflow and implement a sieve. Not sure if it fits on an atmega8a, but should be possible
>>
File: IMG_1094.jpg (495KB, 3000x2000px) Image search: [Google]
IMG_1094.jpg
495KB, 3000x2000px
>>59702990
fuck. Forgot photo.
>>
From math import sumprimesunder

return sumprimesunder (2000000)


Suck it, gaymos
>>
data = read.csv ("savedprimes.csv")
for i in data:
sum+= i <2000000? i,0
>>
>>59700827
With a compiler.
>>
>>59704050
Really? I thought I needed to use a cork-screw!!
Still, I got a segfault. I don't know why.
>>
>>59702144

> return false return true
> Not using ternary operator on numtoAdd
> Qt
> using namespace std;

lel
>>
>>59704050
>>59704277
I had the wrong number in #define. It works.
>>
>>59704356
>> return false return true
whats wrong with this?
>> Not using ternary operator on numtoAdd
I don't know what this is
>> Qt
there is literally nothing wrong with Qt
>> using namespace std;
I'm not typing std:: before every single thing in cout and cin
>>
>>59702990
If you're just doing a naive division test, you can speed it up 3x. See:
https://en.wikipedia.org/wiki/Primality_test#Pseudocode
>>
File: 1490056452566.png (340KB, 858x360px) Image search: [Google]
1490056452566.png
340KB, 858x360px
>>59698274
>asked to sum all prime numbers above two million
>>
>>59698274
>generate all multiples of 6 under 2 million
>check every number one higher and lower than each for primality
>add found primes

Am I doing this right?
>>
File: Képkivágás.png (262KB, 1490x1926px) Image search: [Google]
Képkivágás.png
262KB, 1490x1926px
>>59704356
>>59704521
ok I looked up stuff and improved it based on your comments and some algorithm
how about now
>>
>>59704805
import math

def checkprime(integer):
root = math.sqrt(integer)
j = int(math.floor(root))
if root == j:
return False
else:
for i in range(j):
if i > 1:
if integer % i == 0:
return False

return True

primes = [1,2,3]

i = 6
while i < 2000000:
if checkprime(i-1):
primes += [i-1]
elif checkprime(i+1):
primes += [i+1]

i += 6

print sum(primes)

I get 129230485919 as result
>>
The proper implementation shown below is O(n), amortized.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]){

if (argc != 2){
return 1;
}

int n = atoi(argv[1]);
bool* bools = new bool[n];

bools[0] = true;

for (uint i = 2; i <= n; i++){
if (!bools[i-1]){
for (uint step = (i+i); step <= n; step += i){
bools[step-1] = true;
}
}
}

long sum = 0;

for (uint i = 0; i < n; i++){
if (!bools[i]){
sum += i+1;
}
}

printf("Sum: %ld\n", sum);

delete [] bools;
return 0;
}
>>
>>59704971
> 129230485919 != 142913828922
>>
>>59704971
The answer is 142913828922
>>
>>59705043
I noticed that. However, I can't notice where I made the major errors in my program
>>
>>59698274
Why stop there? Why not sum all the prime numbers?
>>
>>59705058
The error is that the logic of using increments of 6 is wrong.
>>
>>59698572
Sieve of Erathostenes, for example.
>>
>>59705079
But every prime number is one below or above a multiple of 6.
>>
File: 1479057769600.jpg (67KB, 500x500px) Image search: [Google]
1479057769600.jpg
67KB, 500x500px
>>59705097
>But every prime number is one below or above a multiple of 6
what
>>
>>59705137
Besides 2 and 3, this holds true for every prime number possible. If you're writing primality tests, you should probably know about that.
>>
>>59705167
Being +/- 1 from 6*n does not imply primality.
Being prime implies being +/- 1 from 6*n.

A -> B =/= B -> A
>>
So, how do you actually prove that your answer is correct without relying on another computer program which similarly can't prove that it's actually correct?
>>
>>59705258
By formally proving the correctness of your algorithm.
>>
>>59705212
>Being prime implies being +/- 1 from 6*n.
And hence, not being +/- 1 from 6*n implies not being prime.
Therefore, it is only necessary to check 6*n +/-1 for primality.
>>
File: Képkivágás.png (274KB, 1537x1962px) Image search: [Google]
Képkivágás.png
274KB, 1537x1962px
>>59705167
oh my god
this actually works
>>
>>59705058
It could be the case that for some i, 6*i - 1 and 6*1 + 1 are prime.
Your program will only add 6*i - 1 to the sum.
>>
>>59705097
Haha. That's cute :)
>>
ffs I'm not doing project euler for you
>>
>>59704971
if checkprime(i-1):
primes += [i-1]
elif checkprime(i+1):
primes += [i+1]


what if both i - 1 and i + 1 are prime numbers?
>>
>>59704578
printf("the series diverges to infinity.");
>>
>>59698352
> testing every even number
> not knowing that upper bound for a divisor is sqrt(num)
> ugly loop in the end
mfw
>>
>>59705448
https://www.quora.com/Why-do-prime-numbers-always-satisfy-the-6n+1-and-6n-1-conditions-Is-there-any-mathematical-logic-behind-it

You're right, it is cute.
>>
>>59705344
>>59705472
This was the major problem with my script. I fixed everything (I also forgot to test the floored root and included 1 as prime).

import math

def checkprime(integer):
root = math.sqrt(integer)
j = int(math.floor(root))
if root == j:
return False
else:
for i in (range(j)+[j]):
if i > 1:
if integer % i == 0:
return False

return True

primes = [2,3]

i = 6
while i < 2000000:
if checkprime(i-1):
primes += [i-1]

if checkprime(i+1):
primes += [i+1]

i += 6

print sum(primes)

I get the proper result now.
>>
>>59705614
>
root = math.sqrt(integer)
j = int(math.floor(root))
if root == j:
return False

>if the square root of a number is a whole number then it's not a prime



explain please
>>
>>59705674
If a number has a whole number as root, it is divisible by that number, therefore not prime
>>
File: 1463279586045.png (14KB, 166x166px) Image search: [Google]
1463279586045.png
14KB, 166x166px
>>59698748
Quite good.
>>
std::cout << 142913828922 << std::endl;
>>
Rate my sieve of mesopotamia!
#define LIMIT 2000000
#define BITS (sizeof(char) * CHAR_BIT)
int main(void)
{
char *a = calloc(LIMIT / BITS, sizeof(char));
size_t i, j, sum = 0, sq = sqrt(LIMIT);
for (i = 3; i < sq; i += 2) /* skip all even numbers */
if (!(a[i / BITS] & (1 << (i % BITS))))
for (j = i * 2; j < LIMIT; j += i)
a[j / BITS] |= (1 << (j % BITS));
for (i = 2; i < LIMIT; i += (i > 2) ? 2 : 1)
if (!(a[i / BITS] & (1 << (i % BITS))))
sum += i;
printf("%zu\n", sum);
free(a);
return 0;
}
>>
>>59705706
eh
I tried adding it to my code
absolutely no improvement at all
waste of cpu cycle
>>
>>59705884
Try summing all primes up to 20 billion.
Most sieves max out your memory and the naive method will take longer than the heat death of the universe.
>>
>>59705298
Yeah, ok. I can see someone making the assumption that for a natural number N, if N+1 or N-1 are divisible by 6 then the number is prime (which is obviously not true)
>>
>>59698274
function prime_sum_hexa(max)
sum = 5;
for i =6, max, 6 do
if test_prime(i-1) then sum = sum + (i - 1); end
if test_prime(i+1) then sum = sum + (i + 1); end
end
print ("Sum of the first "..max.." primes is: "..sum)
end


Elegant as fuck
>>
>>59706849
You didn't define test_prime
That would be a failing grade in most CS classes.
>>
>>59705777
What's "CHAR_BIT"? Where is it declared?
>>
>>59706849
#include <iostream>

using namespace std;

int main() {
long int a;
int n;
int r=1;
int t[2000000];
for (n=2; n<=2000000; n++) {
bool b=true;
int k=1;
while (t[k]*t[k]<=n && k<r) {
if (n % t[k] == 0) {
b = false;
break;
}
k++;
}
if (b) {
a += n;
t[r]=n;
r++;
}
}
cout << a << endl;
return 0;
}
>>
Sorry, /g/, I'm not learned.

What about an array of bool (1=prime else 0), apply sieve to sqrt(2milion), than sum index to a total if n[index]==1?

Should this method be O(n) ?
>>
>>59707471
I don't even get you what you are doing.

HOW do you get the array of bools where you know something is a prime BEFORE the sieve?

WHICH variant of the sieve (true? optimized? with wheels? with priority queue and wheels?) do you even want to "apply to" sqrt(2million)? Do you mean you want to run the sieve algorithm "up to" that number (and why? numbers higher than sqrt(2million) can actually be primes).
>>
public class Main {

public static void main(String[] args) {
Tools tools = new Tools();
for (int i = 2; i < 2000000; i++) {
if (tools.isPrimenumber(i)){
System.out.println(i);
}
}
}

}


public class Tools {
boolean isPrimenumber (int number) {
boolean primenumber = true;
for (int counter = 2; counter < number; counter++) {
if (number % counter == 0) {
primenumber = false;
}
}
return primenumber;
}
}
>>
>>59698352
That code will be running until the heat death of the universe.
>>
File: 1423508580007.png (37KB, 469x432px) Image search: [Google]
1423508580007.png
37KB, 469x432px
>>59705137
>the atomics of mathematics is the number of the devil

Really makes me think
>>
>>59708247
>different class for a single function
>testing even numbers for primes
>printing out primes instead of summing them
try again
>>
File: 1491144582808.jpg (62KB, 640x960px) Image search: [Google]
1491144582808.jpg
62KB, 640x960px
>he still operates in Stallman mode
>hasn't activated Terry mode
What's your excuse, /g/?
>>
sum(primesUnderTwoMillion);

Duh. Easy.
>>
n = 2000000

sieve = [True] * n
sieve[0] = False
sieve[1] = False

for i in xrange(2, int(n**0.5)+1):
if sieve[i] == True:
for j in xrange(i+i, n, i):
sieve[j] = False

primesum = 0
for i, x in enumerate(sieve):
if x == True:
primesum += i

print primesum

>>
>>59708372
cringe
>>
>>59708441
Very nicely done.
>>
Sieve of guy-with-name-that-starts-with-an-A
>>
>>59702579
Check my Miller-Rabin:
def is_prime(n, k=10):
"""
Miller–Rabin primality test, deterministic for n < 2^64, else
probabilistic with probablity at most 4^-k.

Return True if prime, False if composite.
"""
if n == 2:
return True
if not n & 1 or n < 2: # n & 1: check if even, faster than modulo
return False
rand_base = False
if n < 2047:
k = (2,)
elif n < 9080191:
k = (31, 73)
elif n < 4759123141:
k = (2, 7, 61)
elif n < 1122004669633:
k = (2, 13, 23, 1662803)
elif n < 3474749660383:
k = (2, 3, 5, 7, 11, 13)
elif n < 18446744073709551616:
k = (2, 325, 9375, 28178, 450775, 9780504, 1795265022)
else:
k = range(k)
rand_base = True

def is_composite(x, a, r, n):
if x == 1:
return False
for i in range(r):
if x == n - 1:
return False
x = (x*x) % n
return True

r, d = 0, n - 1
while not d % 2:
d >>= 1 # Devide by 2, faster than //
r += 1
for i in k:
a = randint(2, n - 2) if rand_base else i
x = pow(a, d, n)
if is_composite(x, a, r, n):
return False
return True
>>
>>59708777
>python 2
>>
>>59700316
>2017
>not making a program that just copies over itself with code from stack overflow that already does this
>>
File: 1466278067957.png (261KB, 1048x1024px) Image search: [Google]
1466278067957.png
261KB, 1048x1024px
>>59698274
echo $[$(seq 2 2e6|factor|sed 's/.*: //;/ /d'|tr \\n +|sed 's/+$//')]
>>
const start = Date.now()
const primes = [3]
let total = 5
const primeCheck = (n) => !primes.some(e => !(n%e))
for (let i = 5, q = 7; q<=2000000; i+=6, q+=6)
{
if (primeCheck(i)) {
primes.push(i)
total += i
}
if (primeCheck(q)) {
primes.push(q)
total += q
}
}
console.log('ran for ' + ((Date.now()-start)/1000) + 's')
console.log(total + ' ' + (142913828922 === total))


Output:
ran for 40.856s
142913828922 true


Send hate.
>>
>>59709167
>40.856s
Trial division in Python is faster. This is trash, I'm afraid.
>>
>>59709167
not familiar with the syntax, but I'm assuming your primality test checks, for an n, whether any prime p < n divides n?
If you somehow capped that to check only up until p < sqrt(n) you might get an acceptable runtime.
>>
>>59709341
Javascript running in the browser. It runs on a single core and does a lot of type coercion. I just wanted to test out that anon's +- 1 from multiples of 6 statement.

Doing a port of >>59700072 gives better results of course.
const start = Date.now()
const limit = 2 * (10**6)
const rep = function(src, what, L){
while(L) src[--L]= what;
return src;
}
const getPrimes = (limit) => {
let sieve = rep([], false, limit)
let primes = [];
for (let i = 2; i<limit;i++) {
if (!sieve[i]) {
primes.push(i)
for (let j = i**2; j < limit; j+=i) {
sieve[j] = true
}
}
}
return primes
}
const primes = getPrimes(limit)
let total = 0
primes.forEach(e => total += e)
console.log('ran for ' + ((Date.now()-start)/1000) + 's')
console.log(total + ' ' + (142913828922 === total))


Output: ran for 0.521s 
142913828922 true
>>
jlang
+/ p: i.2*10^6
>>
>>59709719
const start = Date.now()
const limit = 2 * (10**6)
const getPrimes = (limit) => {
let sieve = []
let primes = []
for (let i = 2; i<limit;i++) {
if (!sieve[i]) {
primes.push(i)
for (let j = i**2; j < limit; j+=i) {
sieve[j] = true
}
}
}
return primes
}
const primes = getPrimes(limit)
let total = 0
primes.forEach(e => total += e)
console.log('ran for ' + ((Date.now()-start)/1000) + 's')
console.log(total + ' ' + (142913828922 === total))


Output: ran for 0.172s 
142913828922 true


I feel really dumb for not remembering that Javascript's arrays are always sparse, making the array building needless.
>>
>>59708762
You should see his laptop
I'm sure he's being ironic but I can never be sure
Lurk the sticker threads and look out for velcro patches and a 4chan clover sticker
I'd post it but I'm on my phone
>>
>>59698274
import math

rang = 2000000

xaxis = range(2,int(math.sqrt(rang)))
primeGrid = range(rang)

jj = 2
for ii in xaxis:
while jj*ii < rang:
primeGrid[ii*jj] = False
jj += 1
jj = 2

print sum([x if x else 0 for x in primeGrid][2:])
print sum([1 if x else 0 for x in primeGrid][2:])
[\code]
>>
File: 1476997610740.png (793KB, 1024x578px) Image search: [Google]
1476997610740.png
793KB, 1024x578px
>asked to sum all prime numbers over two million
>>
>>59710816
You wouldn't import a gf
>>
>>59709165
Best post.
>>
>>59698274
Why did you post this same thread 3 times? Fuck off you retarded piece of shit
Thread posts: 146
Thread images: 16


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.