sds2019 icon indicating copy to clipboard operation
sds2019 copied to clipboard

Problem 0.3.3 (How does sigma sum notation translate into code?)

Open Toromash opened this issue 5 years ago • 3 comments

I have an issue understanding the description of problem 0.3.3. I have defined the function and tried to loop it, but there is clearly something wrong with my coding. There is something wrong with the structure, and should i return value twice?

def natural_logarithm(x, k_max): total = 0 for k in range (0, k_max): total += k total = total * 2 return (1/2k_max+1)((x-1)/((x+1))**2*k_max+1) answer_033 = natural_logarithm(2.71, 1000)

Toromash avatar Aug 03 '19 15:08 Toromash

For a start what you have pasted is not valid python code. Read this if you don't know how to type formatted code on github.

Other than that, do you get the sum-notation (otherwise read here)? It implies to add up the terms under the sigma - which is not what you are doing right now.

kristianolesenlarsen avatar Aug 04 '19 09:08 kristianolesenlarsen

yeah, i just copy pasted the code directly in. Sorry about that.

If i use the sum-notation, it will say that ´ínt´ object is not iterable. I don´t have a list of numbers, so i don´t see how it can work?

Toromash avatar Aug 04 '19 12:08 Toromash

Not the python sum function, the mathematical one, sorry if that was unclear. You are right that sum expects an iterable object, so to use it you would first need a list of numbers. Creating the right list of numbers and using sum is a completely valid solution.

About

I have an issue understanding the description of problem 0.3.3.

The code you pasted doesn't correspond to what the mathematical expression is telling you to calculate. In other words your translation from math to code is wrong - this is why i gave you a link that explains the sigma sum notation. Consider beginning by coding up a far simpler sum (e.g. the sum of integers below some k_max) to get a better intuition for the relation between math and code.

kristianolesenlarsen avatar Aug 04 '19 14:08 kristianolesenlarsen