Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Open Language TYS
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: 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:
scheme:

(define (sum low high)
    (/ (- (* high (sub1 high)) (* (sub1 low) (- low 2))) 2))


Are we supposed to take into account negative numbers too, I suppose?
Clayton




PostPosted: 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 Razz
Hikaru79




PostPosted: 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 Razz


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 Razz
wtd




PostPosted: Thu Feb 15, 2007 12:52 pm   Post subject: RE:Open Language TYS

No no... you got the right answer. Smile
Clayton




PostPosted: 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 Razz
Cervantes




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: