Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write a program to find the sum of all odd numbers between a given range [a,b]. Input consists of a single line containing two integers a and b separated by a space. 0 ≤ a,b ≤ 1,000,000,000,000. Your program should not take more than 10 secs.

Sample Inputs
1 5
4 8
1 8
0 5

Sample Outputs
9
12
16
9
share|improve this question
13  
sigh ... ... ... – Joey May 6 '11 at 15:53

7 Answers

up vote 9 down vote accepted

Ruby, 43 characters

p eval"-(%d/2)**2+((%d+1)/2)**2"%gets.split

Example for largest input:

$ time echo "1 10000000000000000" | ruby oddsum.rb
25000000000000000000000000000000

real    0m0.006s
user    0m0.000s
sys     0m0.000s
share|improve this answer
You misspelled evil there. Otherwise, nice work! – Lars Haugseth May 8 '11 at 8:40

Funciton

Not a language particularly suitable for golfing, but I gave it my best shot to cram everything together in best code-golf tradition. Near the end I was making rearrangements that gave net savings of a single character until I could find no more ways to improve it :)

This is 398 characters (or 786 bytes in UTF-16). I challenge anyone to find a way to make it smaller :)

 ┌───┐    ┌──────────┐
╔╧╗┌─┴╖┌─╖│╔═╗┌──╖┌─╖│
║ ║│>>╟┤_╟┘║1╟┤>>╟┤♯╟┘
╚═╝╘╤═╝╘═╝ ╚═╝╘═╤╝╘═╝
    └┐ ┌─┐ ╔══╗┌┴───┐
╔══╗┌┴╖│┌┴╖║32║│ ┌─╖│
║21╟┤×╟┘│♯║╚╤═╝└─┤×╟┘
╚══╝╘═╝ ╘╤╝┌┘ ┌─┐╘╤╝
┌──────┬─┘┌┘ ┌┴╖└─┘
│╔═╗  ┌┴╖┌┘  │−╟┐┌───────╖
│║0║ ┌┤ʘ╟┘┌─┐╘╤╝└┤int→str╟
│╚╤╝ │╘═╝┌┴╖└─┘  ╘═══════╝
│┌┴╖╔╧╗┌─┤×╟┐
└┤ʃ╟╢ ║│ ╘═╝│
 ╘╤╝╚═╝└┬───┘
  │┌─╖┌─┴╖╔═╗╓─╖┌───────╖
  └┤_╟┤>>╟╢1║║_╟┤str→int╟
   ╘═╝╘══╝╚═╝╙─╜╘═══════╝
share|improve this answer
What about BOCU-1 or SCSU? Wouldn't those make it quite a bit smaller? – Joey May 9 '11 at 12:35
@Joey: Certainly not the character count. – Timwi May 9 '11 at 22:54
Ah, but the byte count ;-). Ok, I only now understood how you meant that. – Joey May 9 '11 at 22:58

Haskell - 65

f[l,h]=print$sum$filter odd[l..h]
main=getLine>>=f.map read.words
share|improve this answer
With the change in the task I guess this one takes too long to execute. – Joey May 7 '11 at 6:37

Windows PowerShell, 84 61 55

$a,$b=-split"$input+1"|iex|%{($_-band-2)/2}
$b*$b-$a*$a

O(1) runtime.

share|improve this answer

Golfscript - 12 chars

~)2/2?\2/2?-

can be done in 11 chars if the order of the inputs is reversed

~)]{2/2?}/-
share|improve this answer

Bash, 37

seq `cat`|grep [13579]$|paste -sd+|bc

53 → 41: Replaced awk '{s+=$1}END{print s}' with paste -sd+|bc. Thanks goes to Dimitre Radoulov's answer to a StackOverflow question.

41 -> 37: Replaced read i;seq $i with seq `cat`

share|improve this answer
You can replace cat with m4, pg, or dd to save a character. – Nabb May 7 '11 at 1:16
Nabb: It's moot now anyway as the change to the task makes looping impossible. – Joey May 7 '11 at 6:34
@Joey, you just need to get one of those quantum computers – gnibbler May 7 '11 at 13:06
@gnibbler, Oh, right. It didn't say on what hardware it shall take 10 seconds ;-) – Joey May 7 '11 at 16:03

Wolfram Alpha (12 16 chars)

[Input]to[Input] sum n*(n%2

See example here.

share|improve this answer
1  
sum n(n%2) 1to10 is 13 chars plus the inputs and works. I don't know if this can be considered a "program", though. – belisarius May 9 '11 at 4:04
1to10 sum n(n%2 is 12 chars and also works. Just shows a warning – belisarius May 9 '11 at 4:07
@belisarius Thanks. I think it is a program, just using a rather high-level programming language. – Peter Olson May 9 '11 at 13:13
@Joey I was not meaning that. Wolfram Alpha defines a language, in the same sense a programming language is defined, and the browser is a valid executable platform. What I mean, is that this "program" is not accepting an input file. Just that. – belisarius May 9 '11 at 13:36
belisarius: Sorry, then. So it's just invalid according to the specs. Probably it's not a good choice for most problems here, then. – Joey May 9 '11 at 13:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.