Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168704
Image: ubuntu2004
""" SAGE has a large list of built-in functions and a facility for user defined functions. In this worksheet we shall see how to access the built-in functions, how to add user defined functions and how to see more information about them. A list of user defined functions can be found in the SAGE REFERENCE MANUAL. """
#Built-in function: a demonstration. is_prime?

File: /usr/local/sage2/local/lib/python2.6/site-packages/sage/rings/arith.py

Type: <type ‘function’>

Definition: is_prime(n)

Docstring:

Returns True if n is prime, and False otherwise.

AUTHORS:

  • Kevin Stueve kstueve@uw.edu (2010-01-17): delegated calculation to n.is_prime()

INPUT:

  • n - the object for which to determine primality

OUTPUT:

  • bool - True or False

EXAMPLES:

sage: is_prime(389)
True
sage: is_prime(2000)
False
sage: is_prime(2)
True
sage: is_prime(-1)
False
sage: factor(-6)
-1 * 2 * 3
sage: is_prime(1)
False
sage: is_prime(-2)
False

ALGORITHM:

Calculation is delegated to the n.is_prime() method, or in special cases (e.g., Python int``s) to ``Integer(n).is_prime(). If an n.is_prime() method is not available, it otherwise raises a TypeError.

""" Help returns four fields: File: name of file implementing the object Type: type of object Definition: Shows how the function is called Docstring: Displays the documentation string that was placed in the source code. """
a = factorial(90) + 1 is_prime(a)
False
#How big is 90!+1? len(str(a))
139
#A sample of functions, syntax, recursion and more. def example1(num1, num2): """ Returns the product of the numbers """ ans = num1*num2 return ans
a = example1(87,5)
example1?

File: /tmp/tmpYyP2xw/___code___.py

Type: <type 'function'>

Definition: example1(num1, num2)

Docstring:




    Returns the product of the numbers
print a
435
def example2(num1): """ Prints the Fibbonaci numbers smaller than num1 """ a, b = 0, 1 #multiple assignments a = 0, b = 1 while b < num1: print b, #with the "," it prints on one line; without it is printed on separate lines a, b = b, a+b
example2(30)
1 1 2 3 5 8 13 21
example2(980)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
def example3(num1, num2): """ Returns the largest number. Indentation critically important! """ if num1 > num2: ans = num1 else: ans = num2 return ans
aq = example3(106, 89) print aq
106
#Just another call b1 = example3(23, example3(45,12)) print b1
45
#A recursive example def mygcd(a,b): """ Returns the greatest common divisor of the integers a and b """ if a*b == 0: answer = a + b else: answer = mygcd(b, a%b) return answer
fd = mygcd(879, 360) print fd
3
#In how many ways can you write n as a sum of k positive integers def sumofIntegers(n,k): """ Returns the number of different ways the positive integer n\n can be expressed as a sum of k positive integers. """ if (k > n) or (k <= 0): ans = 0 elif (k == 1) or (k == n): ans = 1 else: ans = sumofIntegers(n-k, k) + sumofIntegers(n-1, k-1) return ans
x = sumofIntegers(11,3)
print x
10
x = sumofIntegers(11,4) print x
11
y = sumofIntegers(23, 4) print y
94
a = 2^127 + 1 is_prime(a)
False
def IntSize(n): """ Returns the number of digits in the decimal representation of the positive integer n. """ k = 0 while n > 0: k = k + 1 n = (n - n%10)/10 return k
IntSize(factorial(90))
139
x = factorial(90) + 1 is_prime(x)
False
def mystery(n): """ returns prime greater or equal to n """ if is_prime(n): return n return mystery(n+1)
mystery(9)
11
mystery(6000)
6007
#booleans a = true; b = false; a and b; a or b;
False True
for x in (false, true): for y in (false, true): if (x and not y): print "x = ",x,"y=",y
x = True y= False
#Boolean functions def nand(a,b): x = not (a and b) return x
a = true; b = false; nand(a,b)
True
nand(a,a)
False
nand(b,b)
True
#the xor function def xor(a,b): x = (a == b) return not x xor(a,b)
True
#The implies function def implies(a,b): x = true if (a == true) and (b == false): x = false return x implies(a,b); implies(b,a)
False True