19
\$\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, 2016 at 5:29

44 Answers 44

1
2
1
\$\begingroup\$

J, 11 bytes

3!1 OR>.@%:

Pretty straightforward. Uses the Dennis formula.

Attempt This Online!

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

FunStack alpha, 46 bytes

Sum Greater? @0 takewhile Square Inc Double #N

Try it at Replit!

Explanation

Compute the infinite list of odd square numbers:

                  #N  Infinite list of natural numbers
           Double     Each times 2
       Inc            Each plus 1
Square                Each squared

Sum those that are less than the input:

                takewhile  Take elements from the list while this function is true:
             @0             First program argument
    Greater?                is greater than the list element
Sum                        Sum those elements
\$\endgroup\$
1
\$\begingroup\$

Scala, 54 bytes

Golfed version. Try it online!

def f(n:Int)=(1 to n by 2).map(x=>x*x).filter(_<n).sum

57 bytes version:

def f(n:Int)=(1 to n by 2).map(x=>x*x).takeWhile(_<n).sum
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 S-, 5 bytes

ƭRœɗ²

Thunno 2, 7 bytes

⁻ƭRœɗ²S

Alternatively, a port of Dennis's Jelly answer:

ƭṃ1Æ|3c

Explanation

ƭRœɗ²  # Implicit input
       # - flag decrements the input
ƭ      # Square root of (input - 1)
 R     # Pop and push [1..that]
  œɗ   # Only keep odd numbers
    ²  # Square each number in this list
       # S flag sums the list
       # Implicit output
ƭṃ1Æ|3c  # Implicit input
ƭṃ       # Push ceil(sqrt(input))
  1Æ|    # Bitwise OR with 1
     3c  # Get nCr with r=3
         # Implicit output

Screenshot

Screenshot

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

Python 2, 49 bytes

This ended up being shorter than a lambda.

x=input()
i=1;k=0
while i*i<x:k+=i*i;i+=2
print k

Try it online

My shortest lambda, 53 bytes:

lambda x:sum((n-~n)**2for n in range(x)if(n-~n)**2<x)
\$\endgroup\$
0
\$\begingroup\$

Python 3, 61 49 bytes

lambda n:sum(i*i for i in range(1,n,2)if i*i<n)

plenty of savings, thanks for helping me out

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to PPCG! Unnamed functions are acceptable too, so you can omit the c (unless you need it for recursion, which you don't). \$\endgroup\$ Apr 21, 2016 at 15:07
  • \$\begingroup\$ @MartinBüttner okay thanks for the help \$\endgroup\$
    – 8BitTRex
    Apr 21, 2016 at 15:11
  • \$\begingroup\$ You can omit the square brackets. This makes it the sum of a generator, which still works. \$\endgroup\$
    – mbomb007
    Apr 21, 2016 at 15:13
  • \$\begingroup\$ i**2 is longer than i*i, and you can shorten if i**2<n else 0 by putting the condition at the end: lambda n:sum(i*i for i in range(1,n,2)if i*i<n). You also don't have to show the old code, we can see that in the version history. \$\endgroup\$
    – orlp
    Apr 21, 2016 at 15:22
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, 2018 at 8:28
  • \$\begingroup\$ @TuukkaX Indeed it works (I never think of that to be honest.) :) \$\endgroup\$
    – Sake
    May 30, 2018 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 and acknowledge you have read our privacy policy.

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