Java :: Aufgabe #276 :: Lösung #1
1 Lösung

#276
Zwei-Quadrate-Satz (Fermat / Girard)
Anfänger - Java
von JKooP
- 06.04.2020 um 14:49 Uhr
1. Schreibe eine Funktion bzw. Methode mit der es möglich ist zu überprüfen, ob es sich bei der eingegebenen Zahl um eine Primzahl handelt.
2. Erweitere das Programm dahingehend, dass nur die ungeraden Primzahlen ausgegeben werden, welche dem Quadrate- Satz von
Fermat entsprechen. Also der Summe zweier ganzzahliger Quadrate.
Beispiele:
5 = 1² + 2², 13 = 2² + 3², 17 = 1² + 4², …
Gegenbeispiele:
3, 7, 11, 19 funktionieren nicht, da sie durch 4 geteilt (Modulo 4) nicht den Wert 1 ergeben. Daher gibt es auch keine Lösung (4n+1).
3. Erweitere das Programm wiederum, sodass die möglichen Lösungen aus als Zahlenpaare auf dem Bildschirm ausgegeben werden.
z.B.: 13 (2,3)
Viel Spaß
2. Erweitere das Programm dahingehend, dass nur die ungeraden Primzahlen ausgegeben werden, welche dem Quadrate- Satz von
Fermat entsprechen. Also der Summe zweier ganzzahliger Quadrate.
Beispiele:
5 = 1² + 2², 13 = 2² + 3², 17 = 1² + 4², …
Gegenbeispiele:
3, 7, 11, 19 funktionieren nicht, da sie durch 4 geteilt (Modulo 4) nicht den Wert 1 ergeben. Daher gibt es auch keine Lösung (4n+1).
3. Erweitere das Programm wiederum, sodass die möglichen Lösungen aus als Zahlenpaare auf dem Bildschirm ausgegeben werden.
z.B.: 13 (2,3)
Viel Spaß
#1

von Matthias L (60 Punkte)
- 17.07.2020 um 00:35 Uhr

import java.util.*; import java.lang.Math; public class SumOfTwoSquares { public static void main(String[] args) { try { int number = Integer.parseInt(args[0]); // Read in the upper limit to find all prime numbers System.out.println(allSquarePrime(number)); // that correspond to Fermat's theorem } catch(Exception e) { System.out.println(e); } finally { } // end of try } public static String allSquarePrime(int max) { // calculates all prime numbers that match the requirement String result =""; for (int i=5;i<max ;i++ ) { if (isPrime(i)&& i%4==1) { result += i +" "+getXY(i); } // end of if } // end of for return result; } public static boolean isPrime(int number) { // Method that checks whether a number is a prime number boolean isP = true; for (int i=2;i<=number/2 ;i++ ) { if (number%i == 0) { isP=false; } // end of if } // end of for return isP; } public static String getXY(int number) { // Method that calculates the squares of the given prime number String result=""; ArrayList<Integer> searchList = new ArrayList<Integer>(); for (int i=1;i*i<number ;i++ ) { // only checks possible squares for this prime number if (searchList.contains(i*i)) { result+=("("+((int)Math.sqrt(number-i*i))+","+(i)+") "); } // end of if else { searchList.add(number-i*i); //adds the associated summand of the current number to the array list } // end of if-else // for example : if the current number is 13 , you can only find the sum pair // between 1 and 3, because 4(square 16) is too big } // end of for // the assosiated summand for 1(square 1) would be 12, and for 2(square4) return result; // it would be 9, so we add those numbers to the list. } // If we reach 3(square 9) we will find the 9 in this lsit and we found the sum pair }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1