diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index 615f647..26ca4a0 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -6,6 +6,7 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; +import java.util.LinkedList; public class TasksFrame extends JFrame { @@ -74,6 +75,68 @@ public class TasksFrame extends JFrame { setupLeibnizR(1000); + setupEulerschePhiFunk(); + + } + + private void setupEulerschePhiFunk() { + + JPanel jPanel = setupPanel("Eulersche Phi Funktion"); + + Button button = new Button("Calc Phi"); + button.setVisible(true); + button.setBounds(120, 50, 100, 30); + + JTextField jTextField = new JTextField(); + jTextField.setVisible(true); + jTextField.setBounds( 10, 40, 60, 20); + + JLabel jLabel = new JLabel(); + jLabel.setVisible(true); + jLabel.setBounds(10, 90, 60, 30); + + + jPanel.add(button); + jPanel.add(jTextField); + jPanel.add(jLabel); + + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int r = ePhi(Integer.valueOf(jTextField.getText())); + jLabel.setText(String.valueOf(r)); + } + }); + + + } + + public int ePhi(Integer n) { + + LinkedList menge = new LinkedList<>(); + + for(int k =1; k<=n; k++){ + if(ggt(k, n)== 1){ + menge.add(k); + } + } + return menge.size(); + } + + public int ggt(int a, int b) { + + int h; + if (a == 0) return 0; + if (b == 0) return 0; + + do { + h = a % b; + a = b; + b = h; + } while (b != 0); + + return a; + } private void setupHelloWorld() { diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index 953a426..c7de36d 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -7,16 +7,14 @@ import static org.junit.jupiter.api.Assertions.*; class TasksFrameTest { @Test - void setupLeibnizR() - { + void setupLeibnizR() { TasksFrame tasksFrame = new TasksFrame(); double x = tasksFrame.setupLeibnizR(100000); assertEquals(Math.PI/4, x, 0.00001, "Die Werte sind unterschiedlich. Soll: 0.785 Ist:" +x); } @Test - void setupDiff() - { + void setupDiff(){ TasksFrame frame = new TasksFrame(); int diff = frame.calcDiff(6, 7); assertEquals(-1, diff); @@ -27,8 +25,7 @@ class TasksFrameTest { } @Test - void setupSumm() - { + void setupSumm(){ TasksFrame frame = new TasksFrame(); int sum1 = frame.calcSumm(23, 11); assertEquals(34, sum1); @@ -39,8 +36,7 @@ class TasksFrameTest { } @Test - void setupSumFromTo() - { + void setupSumFromTo(){ TasksFrame frame = new TasksFrame(); int p1= frame.sumCalc(1,5); assertEquals(15,p1); @@ -50,6 +46,29 @@ class TasksFrameTest { assertEquals(3,p3); } + @Test + void test_ePhi(){ + TasksFrame tasksFrame = new TasksFrame(); + int i = tasksFrame.ePhi( 10); + assertEquals(4, i); + int a = tasksFrame.ePhi(25); + assertEquals(20, a); + int b = tasksFrame.ePhi(15); + assertEquals(8, b); + int c = tasksFrame.ePhi(35); + assertEquals(24, c); + } + + @Test + void test_ggt(){ + TasksFrame tasksFrame = new TasksFrame(); + int ggt1 = tasksFrame.ggt(4, 20); + assertEquals(4, ggt1); + int ggt2 = tasksFrame.ggt(205, 120); + assertEquals(5, ggt2); + int ggt3 = tasksFrame.ggt(262, 599); + assertEquals(1, ggt3); + } @Test void setupArea() {