1 | initial version |
On the "Thematic tutorials" page of the SageMath documentation:
check out this tutorial:
2 | No.2 Revision |
On the "Thematic tutorials" page of the SageMath documentation:
check out this tutorial:
To recap, we start from
n
and m
and we define
We know these sets have a single common point and we want to find it.
Our choice is between:
go upwards
go upwards
Here is the upwards version:
def common_element_upwards(f, g, n, m):
r"""
Return the common element between `A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (1 .. n))
B = (g(i) for i in (1 .. m))
a = next(A)
b = next(B)
while a != b:
if a < b:
a = next(A)
else:
b = next(B)
return a
Here is the downwards version:
def common_element_downwards(f, g, n, m):
r"""
Return a common element between the sets
`A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (n, n - 1 .. 1))
B = (g(i) for i in (m, m - 1 .. 1))
a = next(A)
b = next(B)
while a != b:
if a > b:
a = next(A)
else:
b = next(B)
return a
3 | No.3 Revision |
On the "Thematic tutorials" page of the SageMath documentation:
check out this tutorial:
To recap, we start from
n
and m
and we define
We know these sets have a single common point and we want to find it.
Our choice is between:
go upwards
go upwards
Here is the upwards version:
def common_element_upwards(f, g, n, m):
r"""
Return the common element between `A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (1 .. n))
B = (g(i) for i in (1 .. m))
a = next(A)
b = next(B)
while a != b:
if a < b:
a = next(A)
else:
b = next(B)
return a
Here is the downwards version:
def common_element_downwards(f, g, n, m):
r"""
Return a common element between the sets
`A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (n, n - 1 .. 1))
B = (g(i) for i in (m, m - 1 .. 1))
a = next(A)
b = next(B)
while a != b:
if a > b:
a = next(A)
else:
b = next(B)
return a
4 | No.4 Revision |
On the "Thematic tutorials" page of the SageMath documentation:
check out this tutorial:
To recap, we start from
n
and m
and we define
We know these sets have a single common point and we want to find it.
Our choice is between:
go upwards
go upwards
Here is the upwards version:
def common_element_upwards(f, g, n, m):
r"""
Return the common element between `A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (1 .. n))
B = (g(i) for i in (1 .. m))
a = next(A)
b = next(B)
while a != b:
if a < b:
a = next(A)
else:
b = next(B)
return a
Here is the downwards version:
def common_element_downwards(f, g, n, m):
r"""
Return a common element between the sets
`A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (n, n - 1 .. 1))
B = (g(i) for i in (m, m - 1 .. 1))
a = next(A)
b = next(B)
while a != b:
if a > b:
a = next(A)
else:
b = next(B)
return a
Example:
sage: f = lambda x: x^3 + 1
sage: g = lambda x: x^2
sage: n = 20
sage: m = 30
sage: common_element_upwards(f, g, n, m)
9
5 | No.5 Revision |
On the "Thematic tutorials" page of the SageMath documentation:
check out this tutorial:
To recap, we start from
n
and m
and we define
We know these sets have a single common point and we want to find it.
Our choice is between:
go upwards
go upwards
Here is the upwards version:
def common_element_upwards(f, g, n, m):
r"""
Return the common element between `A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (1 .. n))
B = (g(i) for i in (1 .. m))
a = next(A)
b = next(B)
while a != b:
if a < b:
a = next(A)
else:
b = next(B)
return a
Here is the downwards version:
def common_element_downwards(f, g, n, m):
r"""
Return a common element between the sets
`A = \{ f(i): i = 1 .. n \}`
and `B = \{ g(i): i = 1 .. m \}`.
Assumptions:
- `f` and `g` are increasing functions
- `A` and `B` have a common element
"""
A = (f(i) for i in (n, n - 1 .. 1))
B = (g(i) for i in (m, m - 1 .. 1))
a = next(A)
b = next(B)
while a != b:
if a > b:
a = next(A)
else:
b = next(B)
return a
Example:
sage: f = lambda x: x^3 + 1
sage: g = lambda x: x^2
sage: n = 20
sage: m = 30
sage: common_element_upwards(f, g, n, m)
9
sage: common_element_downwards(f, g, n, m)
9