Computer Science Canada Open Language TYS |
Author: | wtd [ Thu Feb 15, 2007 9:42 am ] |
Post subject: | Open Language TYS |
Write a function which sums an inclusive range of numbers. Bonus points for efficiency. |
Author: | Cervantes [ Thu Feb 15, 2007 11:54 am ] | ||
Post subject: | RE:Open Language TYS | ||
Maybe I don't understand the question, because here's my solution:
Are we supposed to take into account negative numbers too, I suppose? |
Author: | Clayton [ Thu Feb 15, 2007 12:16 pm ] |
Post subject: | Re: Open Language TYS |
In my eyes, it shouldn't make a difference. Adding 1 + 2 is the same as adding 2 + 1 right? so you just get the range and add all numbers together to find the total, so I'm not sure why you're multiplying there Cervantes ![]() |
Author: | Hikaru79 [ Thu Feb 15, 2007 12:35 pm ] |
Post subject: | Re: Open Language TYS |
Freakman @ Thu Feb 15, 2007 1:16 pm wrote: In my eyes, it shouldn't make a difference. Adding 1 + 2 is the same as adding 2 + 1 right? so you just get the range and add all numbers together to find the total, so I'm not sure why you're multiplying there Cervantes
![]() He's using the identity that the sum of the first n consecutive numbers is n(n+1)/2. This gives us an almost constant-time solution to wtd's TYS, which also leads me to think he meant something else. Cervantes' solution is also what I would have done; it's just too easy ![]() |
Author: | wtd [ Thu Feb 15, 2007 12:52 pm ] |
Post subject: | RE:Open Language TYS |
No no... you got the right answer. ![]() |
Author: | Clayton [ Thu Feb 15, 2007 1:39 pm ] |
Post subject: | Re: Open Language TYS |
Shows how little I know, and how much I need to learn ![]() |
Author: | Cervantes [ Thu Feb 15, 2007 3:31 pm ] |
Post subject: | Re: Open Language TYS |
This reminds me of a problem I liked, stolen from Project Euler: Quote: The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2. There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 0^2 + 1^2 has not been included as this problem is concerned with the squares of positive integers. Find the sum of all the numbers less than 10^8 that are both palindromic and can be written as the sum of consecutive squares. |