CoCalc Shared Filesdebugging.sagews
Author: Vince Knight
Views : 4

This sheet shows an example of how to not code: everything is in one cell so it's hard to debug

def find_primes(n)
"""Find primes up till n"""
for i in range(n + 1):
if is_prime(i):
count += 1

# Let's now get the plot of number of primes:

plot = plot(find_primes(n) for n in range(100), title="Number of primes)
plot

# Now for the other plot

plot = plot(find_primes(n) / n for n in range(100), title="Number of primes divided by N)
plot

Error in lines 1-6 Traceback (most recent call last): File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 def find_primes(n) ^ SyntaxError: invalid syntax
def find_primes(n):
"""Find primes up till n"""
count = 0
for i in range(n + 1):
if is_prime(i):
count += 1
return count

find_primes(8)

4
data = [find_primes(n) for n in range(100)]

p = list_plot(data, title="Number of primes")
p

data = [find_primes(n) / n for n in range(1, 100)]
p = plot(data, title="Number of primes")
p

plot(x^2, x, 0, 100)