-4
\$\begingroup\$

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
\$\endgroup\$
3
  • 22
    \$\begingroup\$ sigh ... ... ... \$\endgroup\$
    – Joey
    May 6, 2011 at 15:53
  • \$\begingroup\$ 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 \$\endgroup\$ Aug 13, 2021 at 3:50
  • 1
    \$\begingroup\$ @Dudecoinheringaahing I strongly disagree. Brute force is not allowed in this challenge because of the time limit. \$\endgroup\$
    – Bubbler
    Aug 13, 2021 at 4:03

6 Answers 6

14
\$\begingroup\$

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╟
   ╘═╝╘══╝╚═╝╙─╜╘═══════╝
\$\endgroup\$
3
  • \$\begingroup\$ What about BOCU-1 or SCSU? Wouldn't those make it quite a bit smaller? \$\endgroup\$
    – Joey
    May 9, 2011 at 12:35
  • \$\begingroup\$ @Joey: Certainly not the character count. \$\endgroup\$
    – Timwi
    May 9, 2011 at 22:54
  • \$\begingroup\$ Ah, but the byte count ;-). Ok, I only now understood how you meant that. \$\endgroup\$
    – Joey
    May 9, 2011 at 22:58
10
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You misspelled evil there. Otherwise, nice work! \$\endgroup\$ May 8, 2011 at 8:40
  • \$\begingroup\$ You can replace (%d+1) with just -%d to save 3 bytes \$\endgroup\$
    – Jo King
    Aug 13, 2021 at 8:14
4
\$\begingroup\$

Windows PowerShell, 84 61 55

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

O(1) runtime.

\$\endgroup\$
3
\$\begingroup\$

Golfscript - 12 chars

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

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

~)]{2/2?}/-
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Vyxal s, 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]
\$\endgroup\$
4
  • \$\begingroup\$ Does not work within the time limit. \$\endgroup\$
    – Bubbler
    Aug 13, 2021 at 4:00
  • \$\begingroup\$ @Bubbler edited \$\endgroup\$
    – Wasif
    Aug 13, 2021 at 4:11
  • 1
    \$\begingroup\$ this doesn't meet the problems I/O requirements (which are overly strict but you still must follow them) \$\endgroup\$
    – hyper-neutrino
    Aug 13, 2021 at 6:58
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Aug 15, 2021 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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