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

Sum all the primes under 2 million or this bird will stab you

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

File: stabby bird.jpg (6KB, 239x250px) Image search: [Google]
stabby bird.jpg
6KB, 239x250px
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]
>>
File: 2017-04-03-012656_130x60_scrot.png (1KB, 130x60px) Image search: [Google]
2017-04-03-012656_130x60_scrot.png
1KB, 130x60px
;_________________;
>>
>>59706889
LOOK AT THIS RETARD
HAAHHAHAHAAH xD
>>
>>59706631
What are trying to do here??
>>
File: 2017-04-03-013436_131x53_scrot.png (1KB, 131x53px) Image search: [Google]
2017-04-03-013436_131x53_scrot.png
1KB, 131x53px
>>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.
Thread posts: 21
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.