Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Last active February 10, 2024 08:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manjeettahkur/131bb6bf2d3fe51c1add to your computer and use it in GitHub Desktop.
Save manjeettahkur/131bb6bf2d3fe51c1add to your computer and use it in GitHub Desktop.
Given the triangle of consecutive odd numbers
Description:
Given the triangle of consecutive odd numbers:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
...
function rowSumOddNumbers(n) {
return Math.pow(n, 3);
}
function rowSumOddNumbers(n) {
return n*n*n
}
@evan-kapantais
Copy link

Thanks :)

I have a question: how do people come up with these solutions?

@ta3113ta
Copy link

ta3113ta commented Jul 3, 2019

Thank you

@AceTheCreator
Copy link

@evan-kepantias the solution was derive by realizing what each row has in common, which turns out the row number is the cube root of the row's sum.
1 is the product of 111 = 1
8 is the product of 222 = 8
27 is the product of 333 = 27
The sum of any row of odd numbers is just the row number cubed.

@ima12816
Copy link

that's smart!

@cihat
Copy link

cihat commented Sep 5, 2020

I've been working more than 1 hour :D , thank you

@VivekEemani
Copy link

the solution was derive by realizing what each row has in common, which turns out the row number is the cube root of the row's sum.
1 is the product of 1_1_1 = 1
8 is the product of 2_2_2 = 8
27 is the product of 3_3_3 = 27
The sum of any row of odd numbers is just the row number cubed.

that's bravoooo broo

@Aintripin
Copy link

pow(n, 3) is smart but here's what I've come up with:

def row_sum_odd_numbers(n):
S_n = int((n * (n + 1)) / 2)
m = S_n + 1
current_number = 1
counter = 0
new_list = []
while counter < m:
new_list.append(current_number)
current_number += 2
print(new_list)
counter += 1
new_list.pop()
new_list_reversed = new_list[::-1]
newest_list = []
newest_list += (new_list_reversed[0:n])
return sum(newest_list)

@Awwal01
Copy link

Awwal01 commented Jul 27, 2022

Nice kata thanks @AceTheCreator for the explanation, I might have never discovered the algorithm. It is 2 lines in SQL

select n*n*n "res"
from nums ;

@GauthamMu
Copy link

Big thanks to @AceTheCreator for the solution of this problem!

@GauthamMu
Copy link

U saved me aprox. 30 mins.

@AnatoliyOstapenko
Copy link

AnatoliyOstapenko commented Sep 6, 2022

Thanks :)

I have a question: how do people come up with these solutions?

There is no possibility for regular developer to come up with it. It went from famous Italian mathematician who spent a lot of time to come up with similar things) And I think he had a lot of free time in that years)

Thanks for safe my time)

@salihefee
Copy link

salihefee commented Oct 15, 2022

was it that simple? this is what i did: :D

def row_sum_odd_numbers(n):
    firstNum = (n*(n-1))+1
    list = [firstNum]
    for x in range(n-1):
        list.append(firstNum+2)
        firstNum+=2
    return sum(list)

@GodfredAsa
Copy link

GodfredAsa commented Jan 17, 2023

def row_sum_odd_numbers(n):
return n **3

@George-Gichuru
Copy link

thank you

@WaleBnji
Copy link

Wow, thank you I never thought about it like that. You needed to have seen me think hard about the possible logic.

@euulu
Copy link

euulu commented Aug 28, 2023

Thanks :)

I have a question: how do people come up with these solutions?

Here is what i found on this topic:
sum of consequent M numbers is (M+1)M/2, so we may know how many numbers were below our ROW:
numbersBelow = ((n-1)(n))/2.
Now we may calculate first number in row:
firstNumberInRow = 2
numbersBelow+1.
So, firstNumberInRow = nn-n+1 and last number in ROW is nn-n+1 + 2(n-1). Let assume that last number before row is x1 and last number in row is x2. It's known that 1+3+5+...+(2k-1) = kk. Sum in row must be x2x2 - x1x1. OUR x1 = (nn-n)/2 and x2 = (nn+n)/2. After some simplification: SUM = nn*n.

@Xlizer1
Copy link

Xlizer1 commented Feb 10, 2024

Nice kata thanks @AceTheCreator for the explanation, I might have never discovered the algorithm. It is 2 lines in SQL

select n*n*n "res"
from nums ;

or in one line using javascript:

const rowSumOddNumbers = n => n ** 3;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment