I created a arithmetic equation lol

I created a arithmetic equation lol
0

#1

@Flopet17 posted this blog page in the penguin slack channel

DISREGARD MY SOLUTION … while my maths equation is correct my interpretation of the problem is wrong and i created a solution based on this … eg If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
i was summing all numbers for 3 eg 3,6,9,and all numbers for 5 eg 5
but change his number to 29 he would have 3,5,6,9,10,12,15,18,20,21,24,25,27
i would be doing 3,6,9,12,15,18,21,24,27, and then 5,10,15,20,25
so i would be doing some numbers twice as i took it as two seperate lists …
oh well back to the drawing board lol

so after reading it … i decided to see if i could write a version of the problem he was talking about
basically it was to sum all the numbers up to and less than a max divisible by the num eg if max was 20 and num was 4 you would have 4,8,12,16 … no 20 as 20 is equal to max so result would be 40
so after doing it i recalled from a challenge @P1xt posted the best solutions for certain problems would be arithmetic solutions rather than using loops and arrays and such … so decided to look at the problem for that angle .
so opened up my Microsoft excel and set out to look at the problem and see if i could see any relationships between the numbers and totals … and spotted one and then converted it to a arithmetic equation
this then i converted to code

I am very happy with the result as i expanded it not just to do one number but to do a list of numbers eg get the total for each number in list then return the sum of all totals … and also to do one for sum all the numbers less or = to max divisible by the num

Im even more happy because pre coding days i would never consider being able to do something like this … i would have thought only very clever people could work out things like this … but here i am now doing it lol lol (and im not the clever)

here is my equation

a*((a/x-1)/2+1)```
 
here is a repl of it in action ....
https://repl.it/GHA4/12

#2

While the general idea of trying to find a curt algebraic solution is nice, you’re still using a loop, so you haven’t completed your own challenge. The code is more complex and no faster, so try to really remove the loop and get that optimisation bonus (O(n) -> O(1)).


#3

Very interesting @JohnL3 congratulations!
I’m going to do soon a blog post with exactly what @lynxlynxlynx was saying :slight_smile:
I’ll keep you posted!


#4

Im dont think code is more complex as all it is is a equation … a*((a/x-1)/2+1)
Performance wise i believe it outstrips loops as max values rise
and even with more numbers added to the test it still performs better
as im not used to testing im adding another repl for my performance testing to be checked
you will notice in math equation i added 7 numbers to be summed up and totaled
while my optimized loop is only dealing with 2 numbers
and the difference is noticable
i can only believe if more numbers were added to the looping function it would get even slower

any thoughts appreciated


#5

You’re still looping over every number, there’s no shortcut. You could use map and reduce to have no explicit loops.

But I also see that you meant the internal loop.


#6

DISREGARD MY SOLUTION … while my maths equation is correct my interpretation of the problem is wrong and i created a solution based on this … eg If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
i was summing all numbers for 3 eg 3,6,9,and all numbers for 5 eg 5
but change his number to 29 he would have 3,5,6,9,10,12,15,18,20,21,24,25,27
i would be doing 3,6,9,12,15,18,21,24,27, and then 5,10,15,20,25
so i would be doing some numbers twice as i took it as two seperate lists …
oh well back to the drawing board lol


#7

Use a Set to keep the members unique and sum at the very last moment.


#8

@JohnL3, @lynxlynxlynx here is the post I was talking about: https://medium.com/@popflorin1705/javascript-coding-challenge-1-follow-up-278dcc1e25c7
I hope that you’ll find it useful!


#9

@Flopet17 Yes i did enjoy the article … it was clear and understandable … if fact i spent a lot of time over the weekend with this as i found it very interesting.

i got off to a bad start though … as it was late Friday night when i came across it and i misinterpreted the problem but from my mistake i learned how to list all the natural numbers below x that are multiples of y by creating a math equation … so was very happy with that

but then i was hooked and needed to fix my misinterpretation … so off i set to see could i do it and would it be similar to your updated solution as i was expecting you to have a non looping solution.
so for two number i could remove my for loop as it was only there for cases of unknown quantities on numbers entered … and now i needed a way of removing duplicate values that were totaled

like yourself i thought 3 * 5 = 15 get total for 15 and deduct it from total of 3,5
so even though it adds a bit extra to formula it still outstrips the looping version especially as the max number increases.

And then i hit a problem … dont know if you realize it or not as you are working with a single test case hard coded into the function … 3,5 works 3,7 works , 7,11 works 3,8
but numbers like 3,9 fail

reason being … 3,5 … 3, 7 …7,11 use all prime numbers
even 3,8 which is one prime and one composite
but not all prime composite pairing pass nor all composite composite pass
and i realized for the number to pass we need to use the lowest common multiple of the two numbers to get accurate results eg 3.5 … 15 is LCM for these numbers 3 9 27 fails as 9 LCM
so to fix this i had to use the Lowest common multiple formula to get the proper number.

This then adds more overhead to this problem depending on whos lowest common multiple formula is used … but even with mine while it slows things down at lower max number it still outstrips at the larger max

anyway thats all i have for now … what are your thoughts ???