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.
>>61542876for i < 2000000:
mixing up your for and while loops there senpai
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
>>61543218require '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?
>>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
>>61547461
holy shit
>>61541722primes() {
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