Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
def add(a,b): s = a + b return s
add(1237868678,389798779845)
391036648523
def add_squares(a): n = len(a)-1 s = 0 for i in [0,1,..,n]: s = s + a[i]^2 return s
a=[10,20,..,1000] add_squares(a)
33835000
def polynomial(a,c): n = len(a)-1 s = 0 p = 1 for i in [0,1,..,n]: s = s + a[i]*p p = c * p return s
polynomial([1,1,1,1,1,1,1],3)
1093
def polynomial_horner(a,c): n = len(a)-1 s = 0 for i in [n,n-1,..,0]: s = a[i] + c* s return s
polynomial_horner([1,1,1,1,1,1,1],3)
1093
def bubble_sort(a): n = len(a)-1 for i in [n-1,n-2,..,0]: for j in [0,1,..,i]: if a[j] > a[j+1]: t = a[j+1] a[j+1] = a[j] a[j] = t print a return a
bubble_sort([23,786,86,46,97,45])
[23, 86, 46, 97, 45, 786] [23, 46, 86, 45, 97, 786] [23, 46, 45, 86, 97, 786] [23, 45, 46, 86, 97, 786] [23, 45, 46, 86, 97, 786] [23, 45, 46, 86, 97, 786]