18
\$\begingroup\$

Write a program or function to output the sum of the odd square numbers (OEIS #A016754) less than an input n.

The first 44 numbers in the sequence are:

1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961, 1089, 
1225, 1369, 1521, 1681, 1849, 2025, 2209, 2401, 2601, 2809, 3025, 3249, 3481, 
3721, 3969, 4225, 4489, 4761, 5041, 5329, 5625, 5929, 6241, 6561, 6889, 7225, 7569

The formula for the sequence is a(n) = ( 2n + 1 ) ^ 2.

Notes

  • Your program's behaviour may be undefined for n < 1 (that is, all valid inputs are >= 1.)

Test cases

1 => 0
2 => 1
9 => 1
10 => 10
9801 => 156849
9802 => 166650
10000 => 166650
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Neither of the close reasons on this are valid reasons to close a challenge... \$\endgroup\$ – user45941 Apr 24 '16 at 5:29

38 Answers 38

1
2
0
\$\begingroup\$

Python, 42 38 bytes

f=lambda n,i=1:i*i<n and i*i+f(n,i+2)
\$\endgroup\$
0
\$\begingroup\$

Ruby, 32 bytes

f=->n,i=1{i*i<n ?i*i+f[n,i+2]:0}
\$\endgroup\$
0
\$\begingroup\$

Pyke, 7 bytes (noncompetitive)

Add up_to function which repeats until the rtn is above inp.

#}hX)Os

Explanation:

#   )   -  While rtn < input:
        -   i = 0
 }hX    -    rtn = ((i*2)+1)**2
        -   i+=1
     O  -  rtn[:-1]
      s - sum(^)

Try it here!

\$\endgroup\$
0
\$\begingroup\$

bash + bc, 86 bytes

s=0
for((n=0;;n++));{
w=`bc<<<"(2*$n+1)^2"`
[ $w -ge $1 ]&&break;
s=$((s+w))
}
echo $s

This piece of code is quite self explaining, as it simply sums up the values. It is not quite a magic code, as formatting it properly helps... The stop condition is not in the header of the loop, but after evaluating the step and before adding, since here we have all the values we need without initalising them beforehand.

\$\endgroup\$
0
\$\begingroup\$

Factor, 58 57 bytes

[| n | n 2 * iota [ 2 * 1 + 2^ ] map [ n < ] filter sum ]

Well, this was simpler than I thought!

[| n |       ! new local var n
    n 2 *    ! double n
    iota     ! fixnum>range
    [ 
        2 *  ! double 
        1 +  ! + 1
        2^  ! square
    ] map    ! each item in range(0, n*2)
    [ 
        n <  ! entries over n
    ] filter ! keep-only 
    sum ]    ! sum the resulting array

Unit tests, if you like:

{ 0 }  [ 1  sum-odds-lt ] unit-test
{ 1 }  [ 2  sum-odds-lt ] unit-test
{ 1 }  [ 9  sum-odds-lt ] unit-test
{ 10 } [ 10 sum-odds-lt ] unit-test
{ 156849 } [ 9801 sum-odds-lt ] unit-test 
{ 166650 } [ 9802 sum-odds-lt ] unit-test
{ 166650 } [ 10000 sum-odds-lt ] unit-test
\$\endgroup\$
0
\$\begingroup\$

Pyt, 6 bytes

⁻√ř²ƧƩ

Port of FryAmTheEggman's Jelly answer.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 51 50 bytes

-1 byte thanks to TuukkaX.

Not sure if I can golf this more...

$s=0;map{$_%2?$s+=$_**2:()}(1..(<>-1)**.5);print$s

map checks whether the current value in the array [1.. sqrt(input-1)] is odd or even. If it's odd, increase $s with the odd number squared.

\$\endgroup\$
2
  • \$\begingroup\$ I don't know Perl yet, but I'm quite sure you can use .5. \$\endgroup\$ – Yytsi Feb 18 '18 at 8:28
  • \$\begingroup\$ @TuukkaX Indeed it works (I never think of that to be honest.) :) \$\endgroup\$ – Paul Picard May 30 '18 at 7:38
0
\$\begingroup\$

dc, 19 bytes

1-v1+2/dd+1+d2-**3/

This uses the closed-form formula ⅓k(2k+1)(2k-1) for the largest k such that (2k-1)² < n

Ungolfed version

As a full program:

#!/usr/bin/dc -f

?                   # input

1-v1+2/             # k
dd+1+d2-            # k, 2k+1, 2k-1
**3/                # multply

p                   # output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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