Sum all the primes under 2 million or this bird will stab you
You posted this last week. Try 2 billion this time.#define LIMIT 2000000000
#define BITS (sizeof(char) * CHAR_BIT)
/* Sieve of Eratosthenes with optimizations
* - bitpacked lookup table (2 billion in 250MB)
* - todo: double memory storage by omitting evens
* - do not check divisiblity for numbers > sqrt of n
* - do not check even numbers other than 2
*/
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;
}
>>59705939#include <stdio.h>
#define LIMIT 2000000
char sieve[LIMIT] = { 0 };
unsigned long long primes[200000];
int count = 0;
void get_primes()
{
unsigned long i, j;
for(i = 2; i < LIMIT; ++i)
{
if(!sieve[i])
{
primes[count++] = i;
for(j = i*i; j < LIMIT; j += i)
{
sieve[j] = 1;
}
}
}
}
int main(){
unsigned long long sum = 0;
unsigned long i;
get_primes();
for(i = 0; i < count; ++i)
{
sum += primes[i];
}
printf("sum: %lld\n", sum);
}sum: 142913828922
real 0m0.030s
user 0m0.028s
sys 0m0.000s
it even does good on a raspberry pi zero:pi@raspberrypi:~ $ time ./a.out
sum: 142891895587
real 0m0.198s
user 0m0.180s
sys 0m0.010s
print('142913828922')
>>59705980
What's the sum of it?
got this one:sum: 95673602693282040
real 0m42.693s
user 0m42.136s
sys 0m0.464s
>literally the fourth thread
>neither bans nor suicides have stopped this faggot yet
>>59706149
I wanted to try with 20 billion but the SSD swapping becomes unbearable.
I only have 2GB after all.$ time ./prime
95673602693282040
real 0m50.925s
user 0m50.284s
sys 0m0.300s
>>59705939
[/code]
Ctrl + C
[/code]
Result :142913828922
<html>
<head></head>
<body> <h1> 142913828922 </h1> </body>
</html>
>>59705939
well that just seems rude and unnecessary mr bird.
>>59705939
https://www.wolframalpha.com/input/?i=sum+of+primes+under+2000000
Thanks, W|A
Can someone help me? I don't know why it doesn't work
[CODE]
#include <stdio.h>
int main()
{
unsigned long long suma = 0;
int i = 1;
int limite = 2000000;
while (i < limite)
{
suma = suma + i;
i = i + 2;
}
printf("%llu", suma);
return 0;
}
[/CODE]
;_________________;
>>59706889
LOOK AT THIS RETARD
HAAHHAHAHAAH xD
>>59706631
What are trying to do here??
>>59706938
pls anon no laff
i got it to do 200k
>>59707030
I tried to do bitpacking in JS and couldn't get started because I don't know how to create an array of 8 bit integers.
What a shit language.
>>59706950
Nevermind, I am completely fucking stupid. My code sums every odd number below 2 million.
I am already stabbed.
btw, how do I post code?
>create list p = {n}, 0<n<[sqrt(N)]
>loop integers x,y in p, y > x, such that for each x > 1, remove all y from list p whence x|y
>return sum(p)
>???
>beanus
>>59707051
>this language is shit because I'm too retarded to know how to use it
kys
>>59706493
Kill yourself, you dumb fucking shit stain.