# Find sum of odd numbers

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

• sigh ... ... ...
– Joey
May 6, 2011 at 15:53
• I've closed this as a duplicate of Sum of primes between given range. The fundamental challenge is the same (generate a range from $a$ to $b$, filter elements that meet a criteria, then sum), and, in my opinion, the two are the same task Aug 13, 2021 at 3:50
• @Dudecoinheringaahing I strongly disagree. Brute force is not allowed in this challenge because of the time limit. Aug 13, 2021 at 4:03

## 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╟
╘═╝╘══╝╚═╝╙─╜╘═══════╝


Edit 13 years later: I’ve made a breaking change in the base library to fix something that has always bothered me. As a result, the above code no longer works. However, that wasn’t the only change, so if I update the code it actually gets shorter. This is 345 characters (or 692 bytes in UTF-16).

 ┌───┐    ┌──────────┐
╔╧╗┌─┴╖┌─╖│╔═╗┌──╖┌─╖│
║ ║│>>╟┤《╟┘║1╟┤>>╟┤♯╟┘
╚═╝╘╤═╝╘═╝ ╚═╝╘═╤╝╘═╝
└┐ ┌─┐ ╔══╗┌┴───┐
╔══╗┌┴╖│┌┴╖║32║│ ┌─╖│
║21╟┤×╟┘│♯║╚╤═╝└─┤×╟┘
╚══╝╘═╝ ╘╤╝┌┘    ╘╤╝
┌──────┬─┘┌┘┌─╖┌─╖│
│╔═╗  ┌┴╖┌┘┌┤−╟┤+╟┘
│║0║ ┌┤ʘ╟┘┌┘╘═╝╘╤╝
│╚╤╝ │╘═╝┌┴╖ ┌─╖│
│┌┴╖╔╧╗┌─┤×╟┐┤》╟┘
└┤ʃ╟╢ ║│ ╘═╝│╘═╝
╘╤╝╚═╝└┬───┘
│┌─╖┌─┴╖╔═╗
└┤《╟┤>>╟╢1║
╘═╝╘══╝╚═╝

• What about BOCU-1 or SCSU? Wouldn't those make it quite a bit smaller?
– Joey
May 9, 2011 at 12:35
• @Joey: Certainly not the character count. May 9, 2011 at 22:54
• Ah, but the byte count ;-). Ok, I only now understood how you meant that.
– Joey
May 9, 2011 at 22:58
• Hey I saw you updated Funciton and I just wanted to give a heads-up — the characters 《 and 》 don't render as monospace, at least on a Mac, so every line those appear on looks pretty bad. Maybe consider replacing them with » and «? Apr 10 at 12:57
• @noodleman Thanks for your message. 1) I’ve already used « and » for list functions (push and pop). 2) I can’t restrict myself to the common denominator of all monospace fonts out there. I tried to do that in the past, which caused me to avoid ♭ even though ♯ was fine everywhere. I no longer do this because it’s too limiting. The JuliaMono font has all of the characters I’m using for Funciton, including 《 and 》. You could define some user CSS in your browser to make code blocks on StackOverflow use JuliaMono. Apr 11 at 13:32

## 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  • You misspelled evil there. Otherwise, nice work! May 8, 2011 at 8:40 • You can replace (%d+1) with just -%d to save 3 bytes – Jo King Aug 13, 2021 at 8:14 ## Windows PowerShell, 8461 55 $a,$b=-split"$input+1"|iex|%{($_-band-2)/2}$b*$b-$a*\$a


O(1) runtime.

## Golfscript - 12 chars

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


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

~)]{2/2?}/-


# Python, 32 bytes

As unnamed lambda function:

lambda a,b:((b+1)/2)**2-(a/2)**2


Did some math to get a direct formula. Proof will follow.

# Vyxals, 7 bytes

›"2ḭ²Ḃ¯


Try it Online!

STACK = [4,8]
›               Increment, [5, 8]
"               Pair  [[5, 8]]
2              Push 2 [[5, 8], 2]
ḭ               Integer Division [[2, 4]]
²               Square [[4, 16]]
Ḃ              Reverse [[16, 4]]
¯               Deltas    [[12]]
Implicit Sum [12]

• Does not work within the time limit. Aug 13, 2021 at 4:00
• @Bubbler edited Aug 13, 2021 at 4:11
• this doesn't meet the problems I/O requirements (which are overly strict but you still must follow them) Aug 13, 2021 at 6:58
• I see that Vyxal has commands for split on spaces, convert string to int, and mapping functions, so I'm sure there's some way to combine those to fulfill the requirements
– Jo King
Aug 15, 2021 at 9:49