[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]

You have exactly 30 seconds to find the sum of all primes

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: 28
Thread images: 4

File: Screenshot_20170724_131356.png (9KB, 210x199px) Image search: [Google]
Screenshot_20170724_131356.png
9KB, 210x199px
You have exactly 30 seconds to find the sum of all primes below 2,000,000.
>>
import primes

muh_primes = primes.primes
sum = 0

for i < 2000000:
if i in muh_primes:
sum += i

print(sum)
>>
>>61541722
I just like to shorten things.
require 'prime'
m=2
(3...200000).step(2){|n|m+=n if Prime.prime?(n)}
puts m
>>
Easy, I'll just go to Project Euler and find the answer from when I did this the first time years ago.
>>
Why are you adding everything to an array beforehand? You're only using the value once, so just add it to a running sum.
>>
>>61542876

for i < 2000000:

mixing up your for and while loops there senpai
>>
File: g.png (12KB, 722x384px) Image search: [Google]
g.png
12KB, 722x384px
did i do right /g/?
can anyone confirm this?
>>
>>61545527
Very nice
>>
>>61545527
This is excellent. paste dat der script anon.
>>
Took a bit longer that 30 seconds
+/{(⍳⍵)×{2=+/0=x|⍨⍳x←⍵}¨⍵}2000000
>>
>>61543218
require 'prime'

Prime.each(2000000).reduce(:+)
>>
1.999.999 * 2.000.000 / 2 = 1.999.999.000.000

How to be better than /g/ without a computer.
>>
>>61541722
(defvar *sieve* nil)

(defun sieve-of-eratosthenes (limit)
(let ((nums (make-array limit :initial-element nil)))
(loop for p from 2 below limit
do (loop for multiple from (+ p p) by p below limit
do (setf (aref nums multiple) t))
finally (return nums))))

(defmacro with-prime-sieve (below limit &rest body)
(declare (ignore below))
`(let ((*sieve* (sieve-of-eratosthenes ,limit)))
(progn ,@body)))

(defun primep (y)
(let ((sieve (or *sieve* (sieve-of-eratosthenes (1+ y)))))
(not (aref sieve y))))

(with-prime-sieve below 2000000
(loop for x from 2 below 2000000
when (primep x)
summing x))

Took ~0.08 seconds.

What do I win?
>>
>>61541722
You would have to do a terrible job to write a program that takes more than 30 seconds to do that.
>>
>yfw the language that everyone shits on is the fastest ITT
>>
:code:

System.out.println("#Hillary2016. I'm with her.");

:code:
>>
>>61545527
I thought I told you to never show your face here ever again. What the fuck is wrong with you asshole! 149K LOC?
>>
File: 1492590161135.png (4KB, 264x117px) Image search: [Google]
1492590161135.png
4KB, 264x117px
>>61545527
optimization pass
>>
>>61547389
fuck you I do what I want
filedropper com / primes
>>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FALSE 0
#define TRUE !FALSE

void make_sieve(long limit, char **sieve_arr) {
if (*sieve_arr == NULL) {
*sieve_arr = malloc(limit);
}
memset(*sieve_arr, FALSE, limit);

for (long i = 2; i < limit + 1; i++)
for (long multiple = i + i; multiple < limit + 1; multiple += i)
(*sieve_arr)[multiple] = TRUE;
}

int main() {
char *sieve_arr = NULL;
make_sieve(2000000, &sieve_arr);

long sum = 0;
for (long i = 2; i <= 2000000; i++)
if (!sieve_arr[i])
sum += i;

free(sieve_arr);
printf("sum: %ld\n", sum);
}


0.11
>>
File: holy shit.jpg (23KB, 480x674px) Image search: [Google]
holy shit.jpg
23KB, 480x674px
>>61547461
holy shit
>>
>>61541722
primes() {
int i;
unsigned long long int sum = 1 + 2;
/* evens except 2 can't be a prime */
for (i = 3; i < 2000000; i+=2) {
if (isPrime(i)) s+= i;
}
return s;
}

isPrime(int n) {
if (n & 1 == 0) return 0;
for (i = 3; i <= sqrt(n); i+=2) {
if (n % i == 0) return 0
}
return 1;
}
>>
>>61547473
>>61547485
That's how you do it asshole. NOT fucking 148K LOC. We're you dropped on your fucking head?
>>
>>61548306
yeah but which one is easier to understand

readability > everything else
>>
Kotlin:
fun main(args: Array<String>) {
var sum: Long = 0
(1..2000000).filter { isPrime(it) }.forEach { sum += it }
println("The total is: $sum")
}

fun isPrime(number: Int): Boolean {
if (number <= 1) return false
return isPrimeRecursive(number, number - 1)
}

tailrec fun isPrimeRecursive(number: Int, divisor: Int): Boolean {
if (divisor == 1) return true
if (number % divisor == 0) return false
else return isPrimeRecursive(number, divisor - 1)
}
>>
>>61545527
I'm surprised that your editor didn't shit itself.
>>
>>61548432
Right?
>>
>>61548636
>his editor can't handle 150k LOC
re-evaluate your choices
Thread posts: 28
Thread images: 4


[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.