Number Theory Explorer
Build an app that explores number theory concepts โ prime checking, GCD/LCM computation, Fibonacci generation, modular arithmetic, and prime factorization.
๐ฏ Learning Goals
- โน Implement Euclidean Algorithm for GCD
- โน Master Prime checking and factorization logic
- โน Understand Modular Arithmetic and sequences
- โน Build multi-panel app navigation with tabs
๐ Why This Matters
Number theory is the foundation of digital security. Every time you buy something online with a credit card, prime numbers and modular arithmetic are being used to encrypt your transaction. It's the 'purest' math with the most critical impact.
๐Understanding Number Theory
Theory MasterclassNumber Theory is the branch of mathematics that studies properties and relationships of integers. Prime Number: A number greater than 1 that has no divisors other than 1 and itself. Examples: 2, 3, 5, 7, 11, 13... To check if n is prime: try dividing by all numbers from 2 to โn. If none divide evenly, it's prime. GCD (Greatest Common Divisor): The largest number that divides both numbers evenly. Euclidean Algorithm: GCD(a, b) = GCD(b, a mod b), repeat until b = 0. Example: GCD(48, 18) โ GCD(18, 12) โ GCD(12, 6) โ GCD(6, 0) โ Answer: 6 LCM (Least Common Multiple): The smallest number that both numbers divide into. LCM(a, b) = (a ร b) / GCD(a, b) Fibonacci Sequence: Each number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... Modular Arithmetic: The remainder when dividing. a mod n = remainder of a รท n. Example: 17 mod 5 = 2 (because 17 = 3ร5 + 2)
Mathematical Foundation
๐จPart A โ Designer View (UI Design)
Open MIT App Inventor โ Switch to Designer view. Follow each step below to build the interface.
Set up the screen with tabs
Set Screen1 title to "Number Theory Explorer". Add a HorizontalArrangement with 4 tabs as buttons: "Prime", "GCD/LCM", "Fibonacci", "Mod Arithmetic" Each tab shows/hides different panels.
Create Prime checker panel
Add a VerticalArrangement named PrimePanel: - TextBox: "Enter a number" (NumbersOnly = true) - Button: "Check Prime" - Button: "Find Factors" - ResultLabel for showing output
Create GCD/LCM panel
Add VerticalArrangement named GCDPanel (Visible = false): - HorizontalArrangement: TextBox A, Label "and", TextBox B - Buttons: "Find GCD", "Find LCM", "Find Both" - ResultLabel for output - StepsLabel to show the Euclidean algorithm steps
Create Fibonacci panel
Add VerticalArrangement named FibPanel (Visible = false): - TextBox: "How many terms?" (NumbersOnly = true) - Button: "Generate Sequence" - ResultLabel (this will show the full sequence)
Create Modular Arithmetic panel
Add VerticalArrangement named ModPanel (Visible = false): - HorizontalArrangement: TextBox A, Label "mod", TextBox N - Button: "Calculate" - ResultLabel
๐งฉPart B โ Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
Tab switching logic
When PrimeTab.Click โ show PrimePanel, hide other 3 When GCDTab.Click โ show GCDPanel, hide other 3 And so on for Fibonacci and Mod tabs. Change the clicked tab color to green, others to gray.
Build Prime checker
When CheckPrimeButton.Click: 1. Set n = number(TextBox.Text) 2. If n < 2 โ "Not Prime" 3. Set isPrime = true 4. Loop i from 2 to sqrt(n): if n mod i = 0 then set isPrime = false, break 5. If isPrime โ show "n is PRIME โ" Else โ show "n is NOT prime" For sqrt, use the sqrt block from Math. For mod, use the "modulo of" block.
Build Prime Factorization
When FindFactorsButton.Click: 1. Set n = number(TextBox.Text) 2. Create empty list "factors" 3. Set divisor = 2 4. While n > 1: While n mod divisor = 0: Add divisor to factors list n = n / divisor divisor = divisor + 1 5. Display factors list Example: 60 โ [2, 2, 3, 5] โ "2 ร 2 ร 3 ร 5"
Build GCD with Euclidean Algorithm
Create procedure "findGCD" with parameters a, b: 1. Initialize steps text = "" 2. While b โ 0: steps = steps + "GCD(" + a + ", " + b + ")\n" temp = b b = a mod b a = temp 3. Return a (this is the GCD) 4. Display steps to show the algorithm in action
Build LCM
When LCMButton.Click: 1. Calculate GCD first using the findGCD procedure 2. LCM = (a ร b) / GCD 3. Display: "LCM(a, b) = " + result
Build Fibonacci generator
When GenerateButton.Click: 1. Set n = number(TextBox.Text) 2. If n <= 0 โ show error 3. Initialize: a = 0, b = 1, sequence = "0" 4. Loop from 2 to n: next = a + b sequence = sequence + ", " + b a = b b = next 5. Display the full sequence
Build Modular arithmetic
When CalculateButton.Click: 1. Set a = number(TextBoxA.Text) 2. Set n = number(TextBoxN.Text) 3. If n = 0 โ show "Cannot divide by zero!" 4. result = a mod n (use "modulo of" block) 5. Display: "a mod n = result" 6. Also show: "a = " + floor(a/n) + " ร " + n + " + " + result
๐งชTesting Your App
- โPrime: 97 is prime, 100 is not (factors: 2ร2ร5ร5)
- โGCD(48, 18) = 6, LCM(48, 18) = 144
- โFirst 10 Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
- โ17 mod 5 = 2, 100 mod 7 = 2
- โTest with large numbers: Is 997 prime? (Yes!)
๐Bonus Challenges
Extra credit โ impress your instructor
- โ Add a 'Generate primes up to N' feature (Sieve of Eratosthenes)
- โ Show GCD steps visually like a flowchart
- โ Add GCD of 3 numbers: GCD(a,b,c) = GCD(GCD(a,b), c)
- โ Calculate Euler's Totient function ฯ(n)