Example of math-thru-programming homework
Read: http://mathworld.wolfram.com/RiemannZetaFunction.html Write: a short program to approximate the Riemann Zeta function, rzeta. Show rzeta(n) where n = 2,3,4,5,6 and compare with results given on the above web page. Your function should match to at least 8 decimal places. Some teachers looking at the above would say "but this is NOT high school level" (because we don't study the Riemann Zeta function in hs). But the point is to learn math notation, just to know what the operators do. In this case, SIGMA is enough. And if you've been studying Python, you know that SIGMAs may be implemented as loops of the form: sum = 0 for i in range(1,n): sum = sum + f(i) return sum where f is some rule, perhaps passed as a parameter (see earlier postings re functions, relations etc.) So exactly how the Riemann Zeta function relates to the Gamma function and prime numbers is just "noise" from the point of view of extracting the info needed to write the program. We're teaching kids how to "dismiss the irrelevant" (which is another definition of thinking). They're learning to eyeball complicated math pages, infested with Greek letters, and not be intimidated. One possible answer: def rzeta(n): # approximate Riemann zeta function sum = 0.0 n = float(n) for i in range(1,100000): sum = sum + 1.0/i**n return sum Testing:
primes.rzeta(2) # math.pi**2/6.0 1.6449240668 primes.rzeta(3) 1.20205690311 primes.rzeta(4) # math.pi**4/90.0 1.08232323371 primes.rzeta(5) 1.03692775514 primes.rzeta(6) # math.pi**6/945.0 1.01734306198
participants (1)
-
Kirby Urner