QUICK. WRITE A PROGRAM THAT PRINTS THE TOTAL OF ALL THE NUMBERS DIVISIBLE BY 3...

QUICK. WRITE A PROGRAM THAT PRINTS THE TOTAL OF ALL THE NUMBERS DIVISIBLE BY 3, 5 AND 7 BELOW 9 THOUSAND BILLION (9_000_000_000_000) AND THE TIME IT TOOK TO CALCULATE IT OR THIS BIRD IS GOING TO STAB YOU IN THE DICK!

Attached: z7kzgpllniux.jpg (412x430, 29K)

Other urls found in this thread:

thoughtco.com/probability-union-of-three-sets-more-3126263
twitter.com/NSFWRedditVideo

REFERENCE ANSWER
27385714285725214285714285

>dude do my homework for me lmao

Attached: 07a940a6a7d1f7d72de62be0a7dbc96ed3ceab1423827a6fd296107a3e35a0ac.jpg (1024x923, 162K)

$ time echo 27385714285725214285714285

real 0m0.000s
user 0m0.000s
sys 0m0.000s

hmmm

Attached: DeepinScreenshot_20180528150720.png (1920x1080, 157K)

BIRDMIN NO

>Elapsed time is 0ms

What can I say, I'm a smart person.

Attached: 1517844733647.jpg (884x574, 78K)

in a REAL programmering languge (javascript)

(function getAnser() { return 27385714285725214285714285; })()

fuck i forgot javascript doesnt support big sized numbers :^)

Attached: 1525152880042.jpg (680x640, 124K)

>no bignums
should have written it in a (((real))) language
(print 27385714285725214285714285)

Come at me twinkletoes.

Attached: comeatmetwinkletoes.jpg (528x384, 91K)

Attached: 'v'.jpg (405x358, 30K)

ouch

cat 'precomputed_9terra_fizzbuzz.txt'
Where is my rewards?

Okay, without cheating it takes 8100ns in my machine

5960ns when optimized, rust 1.26.

BURB PLOX DON DU DIS

...

that reference answer is wrong

shitfuck
;

how much do you get?

main = print $sum [x | x

Might have made the program wrong, but it should be 21985714285725214285714279

The sum of all numbers divisible by 3,5,7 from 1 to 21 would be 3, 6, 7, 10, 13, 14, 15, 18, 21

If you iterate over 3, then 5, then 7, and just add the answers, 21 will be counted twice, as would 15. You need to sum across all numbers, subtract out the overlap, and add back in the overlap where all 3 numbers divide into it (multiples of 105).

The answer in the op is just added up the sums, which is wrong.

output:
27385714285725214285714285

real 0m0.005s
user 0m0.000s
sys 0m0.004s


Program C++17:
#include
#include
#include

template < std::size_t ... N, class T, class D>
void sub_dups(T& num, D& dups, std::size_t v)
{
if(dups.count(v))
return;
if(num >= 9'000'000'000'000)
return;
num -= v * (sizeof...(N) - 1u);
(sub_dups(num, dups, v * N) , ...);
}

int main() {
using namespace boost::multiprecision;
mpz_int lim7 = 9'000'000'000'000ull / 7ull;
mpz_int lim5 = 9'000'000'000'000ull / 5ull;
mpz_int lim3 = 9'000'000'000'000ull / 3ull;

mpz_int sum7 = 7 * lim7 * (lim7 + 1) / 2u;
mpz_int sum5 = 5 * lim5 * (lim5 + 1) / 2u;
mpz_int sum3 = 3 * lim3 * (lim3 + 1) / 2u;

mpz_int sum = sum7 + sum5 + sum3;

std::unordered_set dups;

sub_dups(sum, dups, 3 * 5);
sub_dups(sum, dups, 5 * 7);
sub_dups(sum, dups, 3 * 7);
sub_dups(sum, dups, 3 * 5 * 7);
std::cout

Corrected program and output (probably, I'm drunk):
27385714283573448823243681

real 0m0.005s
user 0m0.005s
sys 0m0.000s


#include
#include
#include

template < std::size_t ... N, class T, class D>
void sub_dups(T& num, D& dups, std::size_t v)
{
if(dups.count(v))
return;
if(v >= 9'000'000'000'000)
return;
num -= v * (sizeof...(N) - 1u);
dups.insert(v);
(sub_dups(num, dups, v * N) , ...);
}

int main() {
using namespace boost::multiprecision;
mpz_int lim7 = 9'000'000'000'000ull / 7ull;
mpz_int lim5 = 9'000'000'000'000ull / 5ull;
mpz_int lim3 = 9'000'000'000'000ull / 3ull;

mpz_int sum7 = 7 * lim7 * (lim7 + 1) / 2u;
mpz_int sum5 = 5 * lim5 * (lim5 + 1) / 2u;
mpz_int sum3 = 3 * lim3 * (lim3 + 1) / 2u;

mpz_int sum = sum7 + sum5 + sum3;

std::unordered_set dups;

sub_dups(sum, dups, 3 * 5);
sub_dups(sum, dups, 5 * 7);
sub_dups(sum, dups, 3 * 7);
sub_dups(sum, dups, 3 * 5 * 7);
std::cout

I see.

21985714285725214285714279

real 0m0.015s
user 0m0.015s
sys 0m0.000s


maxVal = 9000000000000
sumVal = 0
def getSum(div,maxVal):
maxScaled = maxVal // div
return div*(((maxScaled+1)*(maxScaled))//2)

for i in [3,5,7,105]:
sumVal += getSum(i,maxVal)
for i in [15,21,35]:
sumVal -= getSum(i,maxVal)
print(sumVal)

>9 THOUSAND BILLION
You know we have a word for this: 9 trillion. It takes less letters to write it this way.

def sum(n, ps, mul):
if not ps:
num = int(n / mul)
return num*(num+1)*mul/2
return sum(n, ps[:1], mul) + sum(n, 1, ps[0] * mul) - sum(n, ps[:1], ps[0] * mul)

print(sum(9000000000000, [3, 5, 7], 1))

for i in range(0, 9000000000000):
if i % 3 == 0 or i % 5 == 0 or i % 7 == 0:
print(i)

t.code artisan

>9 thousand billion

Attached: Vqr4p1E.png (1515x663, 116K)

it's still counting...
should have used threads

Attached: Screenshot from 2018-05-28 10-20-18.png (1626x666, 120K)

>too retarded to read instructions
lmao

I know there's better ways of doing it ( ), but I wanted to play around with multithreaded programming. I learned quite a bit
package main

import "fmt"
import "math/big"
import "sync"

func sum(total chan *big.Int, allNumbers chan int64) {
sum := big.NewInt(0)
for number := range allNumbers {
sum.Add(sum, big.NewInt(number))
}

total

package main

import "fmt"
import "math/big"
import "sync"

func sum(total chan *big.Int, allNumbers chan int64) {
sum := big.NewInt(0)
for number := range allNumbers {
sum.Add(sum, big.NewInt(number))
}

total

>PRINTS THE TOTAL OF ALL THE NUMBERS [condition] [range]
U wot m8

I can confirm your result.

Also, my fastest implementation is in 25 microseconds.

But English is such a shit that the entire thread would be arguiing between the two definitions of billion or trillion

>total
>sum
I interpreted instructions MY WAY

Attached: MYWAY.jpg (900x675, 105K)

var total = 0;
function myFunction(num) {
total = 0;
for (var i =1; i < num; i++) {

if (i%3 ==0 || i%5 ==0 || i%7 ==0 || i%9 ==0) {
total += i;
}

}

console.log(total);
}

myFunction(9000000000);

don't stab me fkin bird

post runtime

I'm still waiting for it to return myFunction(9000000000000);
lmoa

isn't this Euler 001 or something?

Um, sweeties.
(function getAnser() { return 27385714285725214285714285n; })()

import homewerk

for lazy_newfag in ['his mamas basement', 'ILL-EGAL']:
if stop.being.faggot and learn.how.to.google
print lazy_newfag, 'that wasn't so hard, was it?'
else:
print lazy_newfag, 'failed class'

this is the worst thing I have read all day

Simple.

#include

void main()
{
for(i=0, i

What's this terminal?

Attached: Algorithm.png (565x259, 15K)

kek d

9 thousand billion doesn't fix that. Is it 9*10^15 or 9*10^12?

>DIVISIBLE BY 3, 5 AND 7
>divisible by 3 or divisible by 5 or divisible by 7 or divisible by 9
?

jackie chan

Chaos is so cute.

python3 1 line. Generates correct answer
print(sum(map(lambda x: x*(((9000000000000//x +1)*(9000000000000//x))//2),[3,5,7,-15,-21,-35,105])))

python3 1 line. Generates OP's answer
print(sum(map(lambda x: x*(((9000000000000//x +1)*(9000000000000//x))//2),[3,5,7])))

threads not over yet

If you're using the thousand billion expression it's clear it goes million, thousand million, billion, thousand billion, and so on, so it should be 10^15. But OP is a faggot who isn't internally consistent even.
I mean he wrote "print the total" which doesn't mean fucking anything when no operation is meantioned.

What is this? It looks cute.

Judging by the ridiculous bar it is GNOME Terminal.

Guessing "scratch".

Thanks

floor(9000000000000/3)
+ floor(9000000000000/5)
+ floor(9000000000000/7)
- floor(9000000000000/15)
- floor(9000000000000/21)
- floor(9000000000000/35)
+ floor(9000000000000/105)

Deepin Terminal, actually

I think he just meant all the numbers separately. Also 3, 5 AND 7.

are you 12 ?
You should be above 18 to post here

Are you Indian?

I think he meant the sum.

Not gonna do your homworks, kid

misread the question lol

Can we get a few references for sensible numbers so we can test this shitpost?

First step:
Find the frequency when a number is divisble by 3, 5 and 7.

for i in (1..1000)
puts i if i%3 == 0 && i%5 == 0 && i%7 == 0
end


Turn out it's 105, 210, 315, 420 (blaze it, faggot!), and so on.


Second Step:
Add them up.

p (105..9_000_000_000_000).step(105).reduce :+

And who who care about the runtime when it's written in such a slick way..?!
:^)

>if(number.is_divisible(3 || 5 || 7))

Can't tell if shitpost, or guy I work with

My condolences.

r8 my Erlang solution

Attached: Capture.png (666x45, 3K)

Fucking bird.
from numba import jit

N = int(9e12)

@jit
def count(N):
count = 0
for x in range(N):
count += (x%3 == 0) | (x%5 == 0) | (x%7 == 0)
return count

print(count(N))

Can't get it any faster in Python3. Still takes like 30 hours. Tried cython as well, but numba is faster.

>people itt that can't read and use `or`

dafuq? i thought this is an elite hacking forum

Incorrect. It is a Scandanavian soap sculpting forum.

That is very easy.

I assume by and you mean or, the other is just as trivial.

c=sym(9000000000000);
sum=0;
for i=[3,5,7]
n=floor(c/i)
sum=sum+((n*(n+1))/2)*i;

end
sum


Runtime is basically zero.

If you are looping though all numbers you are retarded.
Retards, learn to listen in math class.

I am embarrassed to post here, seriously.

I don't think op meant the logical and otherwise this is trivial:

3*5*7 = 105
9*10^12 / 105 = 85714285714
(85714285714)*(85714285714+1)/2 = 3673469387773469387755
3673469387773469387755*105 = 385714285716214285714275

damn, i knew i was stupid

This is why I think he didn't mean the sum. It's either "3, 5, or 7" or "total" doesn't mean sum but complete collection. It's barely a programming problem otherwise.

>and otherwise this is trivial
No, it is trivial even if OP meant "or", you can do the exact same thing you just did, just for 3 5 7.

Duplicates though. You'd have to subtract multiples of 15, 21, 35 and 105 and that's... more than three lines of code. Ew.

Actually the OR case isn't much harder, just add together the divisors of 3, 5, 7; subtract away the duplicate divisors of 3*5, 3*7, 5*7; then add back the triplicate divisors of 3*5*7.

for 3
>(3*10^12)*(3*10^12+1)/2 *3 = 13500000000004500000000000
for 5
>8100000000004500000000000
for 7
>5785714285716214285714285
for 15
>2700000000004500000000000
for 21
>1928571428572071428571426
for 35
>1157142857139642857142855

13500000000004500000000000 + 8100000000004500000000000 + 5785714285716214285714285 - 2700000000004500000000000 - 1928571428572071428571426 - 1157142857139642857142855 + 385714285716214285714275
=
21985714285725214285714279

did it, but I think he should have subtracted multiples of 105 twice instead of adding it? No wait, it shows up in every floor before it, so it cancels out, then needs to be added again.

Yeah you are right, you would have to figure out all duplicates and subtract them.
But that it at least faster then the brute force approach.

import divisiblePrimes
divisibleBy(3, 7, 9e12)

var total = 0;
function myFunction(num) {
total = 0;
for (var i =1; i < num; i++) {

if (i%3 ==0 && i%5 ==0 && i%7 ==0 && i%9 ==0) {
total += i;
}

}

console.log(total);
}

myFunction(9000000000);


took about 3 hours

>DIVISIBLE BY 3, 5 AND 7
>divisible by 3 and divisible by 5 and divisible by 7 and divisible by 9
What's up with that 9? And why don't you just %105?

>Someone's deleted a solution

Bros, this is a maths trick, and apparently having seen it before is the real way to answer it.

why is 105 positive ?

Explain this trickery or be prepared to be burned as a witch.

I never knew you can do this with the small gauss sum..?

let sum = 0;
let c = 9000000000000;
[3,5,7].forEach(function(i){
let n = Math.floor(c/i);
sum+=((n*(n+1))/2)*i;
});

The sum of numbers 1 to 10 is ((1+10)/2)*10=55. It works like this: 10+1=9+2=8+3=7+4=6+5. They all average out to 5.5+5.5 You know how many numbers there are in the series, so you multiply the amount by the average value. Works the same for other series (I think they have to be linear though?). 3+6+9+12+15+18+21+24+27+30=((3+30)/2)*10=165
Since these series are all whole multiples of 3, 5, 7, 105 or however you interpret the OP, this trick works on them too. You just need to know how many numbers in the series, which is solved by dividing the end of the series by the step size. (30/3=10)

OP said by 3,5, AND 7. Logic is hard...

You're counting up in multiples of 105 till you reach 9*10^12 - (9*10^12 mod 105)
Factor out 105 and you're counting 1 + 2 + 3 ... + ⌊9*10^12 / 105⌋
The sum of 1+2+...+n is n*(n+1)/2. To derive it, flip the sum backwards and add it to itself and you get (n + 1) + (n-1 + 2) +... (1 + n) = (n+1) + (n+1) + ... (n+1) = n*(n+1); halve it to get the original sum.

>DIVISIBLE BY 3, 5 AND 7
so you mean divisible by 105?

>point already made
sorry, I didn't read the whole thread before posting.

All numbers are summed from 1 to N, for multiples of 3 then 5 then 7. Numbers which are multiples of 2 of these numbers (ie 15*n, 21*n, 35*n) are counted exactly twice, so we need to subtract them out to make sure they are included exactly once.

But when a number is included 3 times, or in other words is divisible by 3, 5 and 7, then all 3 pairs of deletions will trigger (15,21,35 all divide 105=3*5*7), removing it 3 times. This leaves 0 inclusions on multiples of 105.

Therefore, instead of subtracting out multiples of 105 like we did with the pairs, we need to add them back in to be included exactly once, as they were first added 3 times, then removed 3 times.

We are essentially calculating the union of between 3 sets. This pattern is used when expanding unions between multiple sets in set theory and is applied in statistics for probabilities (and here for sums)

thoughtco.com/probability-union-of-three-sets-more-3126263