What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
In mathematics, a continued fraction is an expression obtained through an iterative process of representing a number as the sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on.
A continued fraction is an expression of the form
1
x + ----------
1
y + -------
1
z + ----
...
and so forth, where x, y, z, and such are real numbers, rational numbers, or complex numbers. Using Gauss notation, this may be abbreviated as
[x; y, z, ...]
To convert a continued fraction to an ordinary fraction, we just simplify from the right side, which may be an improper fraction, one where the numerator is larger than the denominator.
Continued fractions can be decomposed as well, which breaks it down from an improper fraction to its Gauss notation. For example:
16 1
-- = 0 + ---
45 45
--
16
We can then begin to decompose this:
1
0 + ----------------
1
2 + ------------
1
1 + --------
1
4 + -
3
So the Gauss notation would be [0;2,1,4,3].
Your challenge today is to implement a program that can do two things in the realm of continued fractions:
1) Given a Gauss representation of a continued fraction, calculate the improper fraction. 2) Given an improper fraction, calculate the Gauss representation.
Challenge Inputs
45
--
16
[2;1,7]
7
-
3
Challenge Outputs
45
-- = [2;1,4,3]
16
22
[2;1,7] = --
7
7
- = [2;2,1,1]
3
Bonus
Display the continued fraction. Mega bonus if you use MathML or LaTeX.
Solution
in Python
def fraction_to_gauss(fraction):
num, *_, den = fraction.split("\n")
den, remainder = int(num), int(den)
gauss = []
while remainder:
num, den = den, remainder
result, remainder = divmod(num, den)
gauss.append(str(result))
print(f"[{gauss[0]};{','.join(gauss[1:])}]")
def gauss_to_fraction(gauss):
first, *other = gauss[1:-1].split(";")
gauss = [int(n) for n in other[0][::-1].split(",")] + [int(first)]
num, den = gauss[0], 1
for g in gauss[1:]:
num, den = den + g*num, num
m = max(len(str(num)), len(str(den)))
print(f"{num:>{m}}\n{'-'*m}\n{den:>{m}}")
fraction_to_gauss("""45
--
16""")
gauss_to_fraction("[2;1,4,3]")
In mathematics, a continued fraction is an expression obtained through an iterative process of representing a number as the sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on.
A continued fraction is an expression of the form
1
x + ----------
1
y + -------
1
z + ----
...
and so forth, where x, y, z, and such are real numbers, rational numbers, or complex numbers. Using Gauss notation, this may be abbreviated as
[x; y, z, ...]
To convert a continued fraction to an ordinary fraction, we just simplify from the right side, which may be an improper fraction, one where the numerator is larger than the denominator.
Continued fractions can be decomposed as well, which breaks it down from an improper fraction to its Gauss notation. For example:
16 1
-- = 0 + ---
45 45
--
16
We can then begin to decompose this:
1
0 + ----------------
1
2 + ------------
1
1 + --------
1
4 + -
3
So the Gauss notation would be [0;2,1,4,3].
Your challenge today is to implement a program that can do two things in the realm of continued fractions:
1) Given a Gauss representation of a continued fraction, calculate the improper fraction. 2) Given an improper fraction, calculate the Gauss representation.
Challenge Inputs
45
--
16
[2;1,7]
7
-
3
Challenge Outputs
45
-- = [2;1,4,3]
16
22
[2;1,7] = --
7
7
- = [2;2,1,1]
3
Bonus
Display the continued fraction. Mega bonus if you use MathML or LaTeX.
Solution
in Python
def fraction_to_gauss(fraction):
num, *_, den = fraction.split("\n")
den, remainder = int(num), int(den)
gauss = []
while remainder:
num, den = den, remainder
result, remainder = divmod(num, den)
gauss.append(str(result))
print(f"[{gauss[0]};{','.join(gauss[1:])}]")
def gauss_to_fraction(gauss):
first, *other = gauss[1:-1].split(";")
gauss = [int(n) for n in other[0][::-1].split(",")] + [int(first)]
num, den = gauss[0], 1
for g in gauss[1:]:
num, den = den + g*num, num
m = max(len(str(num)), len(str(den)))
print(f"{num:>{m}}\n{'-'*m}\n{den:>{m}}")
fraction_to_gauss("""45
--
16""")
gauss_to_fraction("[2;1,4,3]")
Comments
Post a Comment