All published worksheets from http://sagenb.org
Image: ubuntu2004
Problem 1 (Dual Numbers)
The square of a small number is very small indeed; e.g., squaring we get . Let be an idealized small quantity with the property that and . "Numbers" of the form with and real are called dual numbers. (Compare with the complex numbers, which are of the form with ). Dual numbers obey the rules of ordinary algebra, subject to the condition that ; for example,
a. Make a class for calculating with dual numbers. A skeleton is provided below: where it says "pass" you fill in the appropriate definition. Show examples of how each of the overloaded operator works. (Optional: Provide a _latex_ function, if you know LaTeX.)
class DualNum(SageObject):
def __init__(self, a, b=0): self.a = RR(a) self.b = RR(b) def _repr_(self): return "DualNum(%s,%s)" % (self.a, self.b) def __neg__(self): pass def __add__(self, other): pass def __sub__(self, other): pass def __mul__(self, other): pass def __pow__(self, n): pass
b. Make sure that you can add, multiply and substract reals from a dual number correctly. (Simply amend the code above, but provide examples of usage in all cases here below.)
c. Assume that . What dual number does evaluate to? Add the definitions of __div__ and __invert__ to the class DualNum that reflects what you found.
Answer:
Division.
Invert. We know that the inverse of is because . Let´s simplify the inverse.
d. Find a meaningful way of overloading the comparison operators <,<=,==, etc and implement it.
e. Solve the second degree equation in dual numbers.
Answer:
First let´s insert an arbitrary dual number in for .
We know that ...
Now we want our dual number to be equal to zero the real number has to be zero and the factor in front of . Let´s first find out the possible values of .
So we see that either or . Now let´s find out the possible values of given these values of . We have the equation:
We will first use and obtain.
.
So one of our dual numbers is or simply . Now we will use :
There we have another solution which is . It´s easy to verify the solutions, the first solution
And the second solution
f. (Bonus) Write a function that solves the second degree equation where are dual numbers. The function needs to find or show all dual numbers that fulfil a given equation.
Problem 2. (Quaternions)
The quaternions are a generalization of the complex numbers. They have the form where are real numbers and fulfill the following equation:
Note that here the order matters in multiplication: but $ji=-k.
You can read more on Quaterions on Mathworld and Wikipedia.
Write a class for Quaternions and implement all the meaningful operations. Give good examples of how each operator is used.
Problem 3 (Problem 79 on Project Euler).
A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may asked for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
Problem 4 (Problem 71 of project Euler)
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called a reduced proper fraction.
If we list the set of reduced proper fractions for d 8 in ascending order of size, we get:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
It can be seen that 2/5 is the fraction immediately to the left of 3/7.
By listing the set of reduced proper fractions for d 1,000,000 in ascending order of size, find the numerator of the fraction immediately to the left of 3/7.
Answer:
The function loops through all denominators from to and checks just the numerator that gives us the value that is to the left of . The only thing that really needs explanation is how we obtain the numerator. Well we know that our number has to be
We want the numerator to be less than so we take the floor of the right side:
Then we can be sure that we have the numerator of the fraction that is next to the left from given the denominator .
As we can see below the numerator we are looking for is .