From 8331d368695fd2cca02164034e26dbd0186bc4c0 Mon Sep 17 00:00:00 2001 From: belliash Date: Sat, 20 Apr 2019 14:17:06 +0200 Subject: [PATCH] Program to calculate Euler's Totient Function. --- tests/eulers_totient.aer | 29 +++++++++++++++++++++++++++++ tests/eulers_totient.exp | 10 ++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/eulers_totient.aer create mode 100644 tests/eulers_totient.exp diff --git a/tests/eulers_totient.aer b/tests/eulers_totient.aer new file mode 100644 index 0000000..643d8ad --- /dev/null +++ b/tests/eulers_totient.aer @@ -0,0 +1,29 @@ +// A simple C program to calculate Euler's Totient Function +class Program { + + // Function to return gcd of a and b + private int gcd(int $a, int $b) { + if($a == 0) + return $b; + return $this->gcd($b % $a, $a); + } + + // A simple method to evaluate Euler Totient Function + private int phi(int $n) { + int $result = 1; + for(int $i = 2; $i < $n; $i++) + if($this->gcd($i, $n) == 1) + $result++; + return $result; + } + + + // Driver program to test above function + public int main() { + int $n; + for($n = 1; $n <= 10; $n++) + printf("phi(%d) = %d\n", $n, $this->phi($n)); + return 0; + } + +} diff --git a/tests/eulers_totient.exp b/tests/eulers_totient.exp new file mode 100644 index 0000000..563e194 --- /dev/null +++ b/tests/eulers_totient.exp @@ -0,0 +1,10 @@ +phi(1) = 1 +phi(2) = 1 +phi(3) = 2 +phi(4) = 2 +phi(5) = 4 +phi(6) = 2 +phi(7) = 6 +phi(8) = 4 +phi(9) = 6 +phi(10) = 4 \ No newline at end of file