%% Exploring Egyptian Fractions: Ancient Mathematics with Modern Tools
% This MATLAB Live Script explores how ancient Egyptian mathematicians represented rational numbers using sums of distinct unit fractions. The activity connects historical mathematics with algorithmic computation using MATLAB.
% ------------------------------------------------------------------------- Learning Goals: - Understand the concept and historical use of unit fractions. - Implement a decomposition algorithm (Greedy Algorithm) in MATLAB. - Compare ancient and modern approaches to representing rational numbers. - Develop computational and historical thinking skills. -------------------------------------------------------------------------
%% 1. Historical Background
% Ancient Egyptian mathematicians represented fractions as sums of distinct unit fractions (fractions with numerator 1). For example:
% 2/3 = 1/2 + 1/6
% This approach appears in the Rhind Mathematical Papyrus (~1650 BCE), one of the oldest known mathematical documents. The papyrus contains tables of decompositions for fractions of the form 2/n, among others.
% Modern algorithms can reproduce these decompositions. One well-known approach is the Greedy Algorithm for Egyptian Fractions, which always selects the largest possible unit fraction smaller than or equal to the remaining portion.
%% 2. Define the Egyptian Fraction Decomposition Function
% The following function uses the Greedy Algorithm to decompose a rational number p/q into a sum of distinct unit fractions.
% Example of use: egyptian_fraction(5,6)
% This function will output the denominators of the unit fractions.

function egyptian = egyptian_fraction(p, q)
% EGYPTIAN_FRACTION Decompose p/q into Egyptian fractions
%   Uses the Greedy Algorithm for Egyptian Fraction decomposition.
%   Returns a vector of denominators.

    egyptian = [];
    while p ~= 0
        % Find the smallest unit fraction greater than or equal to p/q
        n = ceil(q / p);
        egyptian(end + 1) = n;

        % Update fraction: p/q - 1/n
        p = p * n - q;
        q = q * n;

        % Simplify fraction
        g = gcd(p, q);
        p = p / g;
        q = q / g;
    end
end

%
%% 3. Example Use
% Let's try decomposing 5/6 into its Egyptian fraction components. We expect: 5/6 = 1/2 + 1/3

p = 5; q = 6;
egyptian = egyptian_fraction(p, q);

disp("Egyptian Fraction Decomposition of 5/6:")
for i = 1:length(egyptian)
    fprintf("1/%d ", egyptian(i));
    if i < length(egyptian)
        fprintf("+ ");
    else
        fprintf("\n");
    end
end

%
%% 4. Visualization
% Let's visualize the relative sizes of the unit fractions in the decomposition. This helps us see how smaller and smaller pieces add up to the target fraction.

values = 1 ./ egyptian;
figure;
bar(values, 'FaceColor', [0.2 0.6 0.8]);
title('Visualization of Egyptian Fraction Components');
xlabel('Unit Fraction Index');
ylabel('Fractional Value');
grid on;

%
%% 5. Student Exploration
% Try decomposing other fractions such as 3/7, 4/13, or 7/8. Compare your results to the decomposition of 5/6.
% Example: egyptian_fraction(3,7)
% Reflection Questions: 1. Why does the greedy algorithm sometimes produce long decompositions? 2. How might ancient mathematicians have approached these problems differently? 3. Do different fractions lead to patterns in denominators?
%% 6. Optional Extension – Compare Different Fractions
% Create a simple function to compare decompositions of multiple fractions. This can help students analyze efficiency and structure.

fractions = [3/7; 5/6; 7/8; 4/13];
labels = ["3/7","5/6","7/8","4/13"];
lengths = zeros(size(fractions));

for i = 1:length(fractions)
    [p, q] = rat(fractions(i));
    denom = egyptian_fraction(p, q);
    lengths(i) = length(denom);
end

figure;
bar(categorical(labels), lengths);
title('Number of Unit Fractions Needed for Each Rational Number');
xlabel('Fraction');
ylabel('Number of Terms');
grid on;

%
%% 7. Reflection and Connection
% Consider how this algorithmic process connects ancient reasoning to modern computation.
% Discussion / Writing Prompts: - How do algorithmic approaches mimic or differ from historical reasoning? - What might have motivated the Egyptians to prefer sums of unit fractions? - How does this exercise show that ancient mathematics was both practical and abstract?
% Instructors may ask students to submit: - A brief report or presentation explaining their code and findings. - A reflection connecting computational efficiency and historical context.
%% 8. References (Instructor Resource)
% - Gillings, R.J. (1972). Mathematics in the Time of the Pharaohs. Dover Publications. - Imhausen, A. (2003). "Egyptian Mathematics." In The Mathematics of Egypt, Mesopotamia, China, India, and Islam: A Sourcebook, edited by Victor Katz. Princeton University Press. - Chace, A.B. (1927). The Rhind Mathematical Papyrus: British Museum 10057 and 10058. MAA. - MacTutor History of Mathematics Archive: https://mathshistory.st-andrews.ac.uk/HistTopics/Egyptian_mathematics/ - British Museum Online: https://www.britishmuseum.org/collection/object/Y_EA10057