I was bored and I'm a bit of an arithmetic nerd, so I figured I would make this thread. I'll ask a question about some math trick and you guys can try to answer it and/or post your own. Getting answers with Google is cheating.
What is the fastest way to calculate the square, in order, of the numbers from 1 to 50?
Math Quiz Time
It would depend on platform I suppose. For exmple, on the DSP platforms I use quite often, you have hardware loops, one-cycle instructions and parallel compute-and-move instructions, so you can set up the loop, the necessary registers and calculate all squares in 53 cycles using a single computational unit, or 29 cycles using two computational units, and store the results in a memory buffer just using normal multiplication of the loop index with itself.
But, I suspect that is not what you actually intended with your question, but that you were looking for is some mathematical trick to compute the square of a number using as few simple mathematical constructs as possible (with addition being simpler than multiplication for example).
Takes advantage of the fact that n[sup]2[/sup] is the sum of the n first odd numbers.
But, I suspect that is not what you actually intended with your question, but that you were looking for is some mathematical trick to compute the square of a number using as few simple mathematical constructs as possible (with addition being simpler than multiplication for example).
for(int n=1, delta=3, square=1; n<=51; square+=delta, n+=1, delta+=2) {
std::cout << n << " squared is " << square << std::endl;
}
Takes advantage of the fact that n[sup]2[/sup] is the sum of the n first odd numbers.
I actually was talking about humans doing it, and not computers. But you gave the correct answer.
I would have phrased it in words as:
Add the next odd number to the current value.
Add 1 to 0 to get 1 is the square of 1. Add 3 to 1 to get 4 is the square of 2. Add 5 to 4 to get 9 is the square of 3.
I should have known someone would give me code![smile.png](http://public.gamedev.net//public/style_emoticons/default/smile.png)
After writing that, I realized you gave me an English version under the code, but I didn't notice since the phrasing is not suitably colloquial for my brain.
Next quiz:
How would a human calculate, in their head, the product of two numbers containing an arbitrary number of digits, where all the digits are 9?
An example would be 9999999999 x 99999999 of which the result is 999999989900000001.
I would have phrased it in words as:
Add the next odd number to the current value.
Add 1 to 0 to get 1 is the square of 1. Add 3 to 1 to get 4 is the square of 2. Add 5 to 4 to get 9 is the square of 3.
I should have known someone would give me code
![smile.png](http://public.gamedev.net//public/style_emoticons/default/smile.png)
After writing that, I realized you gave me an English version under the code, but I didn't notice since the phrasing is not suitably colloquial for my brain.
Next quiz:
How would a human calculate, in their head, the product of two numbers containing an arbitrary number of digits, where all the digits are 9?
An example would be 9999999999 x 99999999 of which the result is 999999989900000001.
I take an example using a determined number of 9s to show the steps: 9999*99 = (10000-1)*(100-1) = 10000*100-10000-100+1. Multiplication by 10-exponents is trivial by counting zeros, two trivial subtractions by 1-non-zero-digit numbers (and do the subtraction with the smallest one to not even have to care about borrowing during subtraction), followed by another trivial addition by 1.
Its true that that is a good method and probably the traditional one they teach in school but, its not optimal.
Look at this example:
9*9=81
99*99=9801
999*999=998001
9999*9999=99980001
99*9=891
999*99=98901
9999*999=9989001
99999*9999=99989001
999*9=8991
9999*99=989901
99999*999=99899001
999999*9999=9998990001
9999*9=89991
99999*99=9899901
999999*999=998999001
9999999*9999=9998999001
Look at this example:
9*9=81
99*99=9801
999*999=998001
9999*9999=99980001
99*9=891
999*99=98901
9999*999=9989001
99999*9999=99989001
999*9=8991
9999*99=989901
99999*999=99899001
999999*9999=9998990001
9999*9=89991
99999*99=9899901
999999*999=998999001
9999999*9999=9998999001
I know the pattern, but in my opinion it is more difficult to remember the pattern and replicate the result from a given number of nines than to just use some trivial and universal mathematical tools.
I know the pattern, but in my opinion it is more difficult to remember the pattern and replicate the result from a given number of nines than to just use some trivial and universal mathematical tools.
Do you think that knowing the pattern is faster if you are able to keep track of it? For instance a more general system may be slower, but it can be applied across a wide range of problems, whereas with patterns you have to know the pattern for each specific problem type. Plus general patterns will see more use, this solidifying them in memory.
I think the fact that you have to remember a pattern is worse than applying trivial and universal mathematical tools. The key points here are universal because I don't have to remember anything specific to multiplying numbers of nines, and trivial because it only involves a very simple multiplication of powers of 10 and three equally trivial subtractions/additions. You have to remember different patterns if the problem changes the problem slightly so that one or both series of nines ends with, say, an eight instead, but I can apply my very same trivial and universal tools, and end up with the very same trivial multiplications and subtractions.
Now, if the resulting calculations from my method involves non-trivial multiplications or subtractions, then we can discuss the benefit of patterns and I could agree. But, in my opinion, the triviality of the operations involved simply doesn't justify remembering specific patterns in this case.
Now, if the resulting calculations from my method involves non-trivial multiplications or subtractions, then we can discuss the benefit of patterns and I could agree. But, in my opinion, the triviality of the operations involved simply doesn't justify remembering specific patterns in this case.
Do you think that knowing the pattern is faster if you are able to keep track of it? For instance a more general system may be slower, but it can be applied across a wide range of problems, whereas with patterns you have to know the pattern for each specific problem type. Plus general patterns will see more use, this solidifying them in memory.
Yes, because multiplying numbers with only '9' digits is something I do every day
![smile.png](http://public.gamedev.net//public/style_emoticons/default/smile.png)
I would agree that some patterns are useful to remember, such as [eqn]a^2 - b^2 = (a - b)(a + b)[/eqn] or [eqn]a^2 + 2ab + b^2 = (a + b)^2[/eqn] instead of working through them each time using algebra, since after a while you can recognize them instantly and they come up often, but in this case, what makes the '9' pattern any more special than the '1' or '2' or '5' pattern? And the pattern doesn't come up that often - I don't feel it's worth remembering this one.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement