This is cool. As someone out of full time education, and who dropped maths relatively early, where’s a good place to start learning some of this stuff? By stuff I mean things related to algebra and whatever the black magic I quoted is
You can also start your way up from the basics in Khan's Academy.
The funny thing is that, since then, I've looked at that formula several times and can't for the life of me figure out I got from the above formula to the the sum of the range formula. I guess younger me was smarter than current me.
Let's start with a 4x4 square:
OOOO
OOOO
OOOO
OOOO
Divide it into two triangles:
OOOO
OOO O
OO OO
O OOO
Note that one of the triangles has a side of (n-1) and other has a side of n. Now, let's see how many elements has each triangle. As I said, we can use diagonal lines. So the first diagonal has 1 element:
O
We then add the second diagonal, which has 2 elements (I'll use lower caps for the elements that were already present):
oO
O
The third one has 3 elements:
ooO
oO
O
And finally we add the last one:
oooO
ooO
oO
O
It's easy to see how this procedure can be extended to any triangle and to any square (which can be divided in two triangles).
So we can see that:
1) A triangle of side n has sum(1..n) elements.
2) A square of side n can be decomposed into a triangle of side n and another one of side (n-1).
3) Now you can use some basic algebra to determine the value of sum(n): if sum(n-1) + sum(n) = n^2, and sum(n-1) + n = sum(n), then 2·sum(n) = n^2+n, therefore sum(n) = (n^2+n)/2, or if you prefer, sum(n)=n·(n+1)/2.
n^2 = sum(1..n) - sum(1..n-1) = 2sum(1..n-1) + n
=> n^2 - n = 2sum(1..n-1) => n(n-1) = 2*sum(1..n-1) => sum(1..n-1) = n(n-1)/2
And you can rewrite that as... sum(1..n) = n(n+1)/2
This is an awesome visualization, by the way.
Yes. I meant n^2 = sum(1..n-1) + sum(1..n).
[1] https://www.amazon.com/Magic-Math-Solving-Figuring-Out/dp/04...
Combinatorics is one of my favorite fields I always feel it has a lot of cool gems like that one and the proofs are often really intuitive. Combinatorics is all about counting things like the number of possible poker hands given a single deck or the number of ways you can seat 4 people around a table.
It was already mentioned below but, discrete math is another good one. Discrete math includes combinatorics, graph theory, set theory, logic, and number theory. All of these have really cool little tidbits of information. My Discrete Math course was my favorite class at university so far. One of the first random bits of information I learned in that class was that any number is divisible by 9 if the sum of it’s digits are divisible by 9 this was posed to us in one of our first homeworks to be proven or disproven and we were able to prove it using some algebra.
This looks like it has some ok examples: https://www.tutorialspoint.com/discrete_mathematics/discrete...
S = 1 + 2 + 3 + ... + n-1 + n
+
S = n + n-1 + n-2 + ...+ 2 + 1
=
2S = n+1 + n+1 + n+1 + ... + n+1 + n+1
every column sums to n+1
It's disingenuous to assert that is same as the sum of that infinite series without the associated caveats. By the definitions of infinite series that we all learned in calculus, that is a divergent series with no sum.
Edit: Wolfram Alpha[0] has a good graphic showing why this series converges to -1/12 (the one with the red line, drawing a peach-like shape). It also gives an intuition as to how complex numbers are influencing the results despite being omitted from the equation.
Yes, you can play a nice trick with that sum if you ignore the fact that inf-inf is meaningless.
The math involved in reaching this identity is akin to dividing by zero.
Wikipedia[0], MathWords[1] and Brilliant[2] might be some good resources for you.
[0] https://en.wikipedia.org/wiki/Lists_of_mathematics_topics [1] http://www.mathwords.com/ [2] https://brilliant.org/
Note that the level for which you're aiming, is a level wherein such summation is much closer to triviality, than it is to `black magic` - whatever that means.
sum [x | x <- [1..999], (mod x 3 == 0) || (mod x 5 == 0)]
which is really just bluntly translating the question into a programming language and letting it take care of the rest, your approach involves some actual insight into the underlying math.By the way doesn’t the original problem say exclude multiples of 15?
IMO Python’s generators and coroutines are one of its most useful yet least appreciated features. Being able to suspend a function, resume it, get values from it, send values or exceptions to it is a highly powerful feature. You can use them to implement user-space cooperative threads, write a scheduler for that, implement message-passing concurrency, and do highly efficient multiplexed I/O in the style of asyncio.
This might be fixed some time or never. There is little push for ultimate performance and actually identifying arithmetic forms in recursive loops in the front-end is hard. (Heck it sometimes fails to tail optimize.)
The sum of x for x from one to 999 where x mod three is zero or x mod five is zero.
well, the bit in square brackets is a comprehension. `sum `is just ordinary function application.
I've been working with a continuation-pasing style variant of Joy (https://en.wikipedia.org/wiki/Joy_programming_language https://github.com/calroc/joypy) and tried it on the Project Euler problem 1: https://github.com/calroc/joypy/blob/master/docs/3.%20Develo...
I came up with a solution that generalizes to blocks of terms, but yours is much much nicer! Cheers!
""" Between 0 and 990 inclusive there are sixty-six "blocks" of seven terms each, starting with:
[3 5 6 9 10 12 15]
And ending with: [978 980 981 984 985 987 990]
If we reverse one of these two blocks and sum pairs...
"""The Advent of Code 2017 includes a puzzle involving a "spiral" memory grid and I had a similar thing where I was able to figure out a simple mathematical way to solve the puzzle: https://github.com/calroc/joypy/blob/master/docs/Advent%20of...
I'm not explaining it well 'cause I'm all excited to show it.
" Project Euler problem 1 :-
It helps to know the sum of (1 .. x) is 0.5 * n * (n+1).
Multiples of 3 less than 1000 are (3 .. 999) == (1 .. 333) * 3 = 0.5 * 333 * 334 * 3 = 166833
Multiples of 5 less than 1000 are (5 .. 995) == (1 .. 199) * 5 = 0.5 * 199 * 200 * 5 = 99500
We must subtract common multiples (those of 15), so (15 .. 990) = (1 .. 66) * 15 = 0.5 * 66 * 67 * 15 = -33165
166833 + 99500 - 33165 = 233168
O(1) solution FTW :P Nice looking code is good, but a smart algorithm makes a big difference. "
OK, I was a smug arsehole, but there's a point to be made y'know?