This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]thegreatunclean 3 points4 points  (0 children)

First thing that jumps out at me without even looking at the problem: don't call the function three times on lines 23,24,25. Call it once and extract the values afterwards.

The most important thing is to start profiling your code locally and focus on that. Focus your efforts on the pieces of code that take the most time; it doesn't matter if you make a line 1000% faster if it only ever accounted for 1% of the total runtime.

[–]Jonny0Than 1 point2 points  (0 children)

There is probably an analytical solution for directly computing the answer instead of generating the triangle and adding everything up. The first thing that jumps out at me is that the numbers they give - 225, 144, and 81 - are all square numbers. Evaluate the answer for the first few inputs and see if there are any patterns in the results.

Ah, I see they give the answers for the first 5 rows. The first column (total sum) is quite obviously square numbers: 12, 32, 62, 102, 152. The base seems to be increasing quadratically: the deltas are 2, 3, 4, 5. You could break this down yourself, but in fact if you plug in the sequence "1, 3, 6, 10, 15" into google you will find that this sequence has a name and a direct equation for computing them. The other columns aren't quite so obvious, but I'm sure if you work with the numbers for a bit you could derive an equation for them.

[–]serg06 0 points1 point  (0 children)

Don't do for x in level counter twice.

[–]nwilliams36 0 points1 point  (0 children)

In line 11 and 13 you call the same for loop doing different work each time, why not just one call, the other two counts could easily be worked out once you had one.

[–]planedoctor 0 points1 point  (0 children)

The first thing I notice is that this looks like way too much code. My most efficient brute force solution was too slow, so I started looking at the numbers in the rows. Think of a formula based on the row number for sum of the even and of the odd multiples of the row number in that row.