Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Pythagoräische Zahlentrippel (Schleifen)

Schreiben Sie ein Programm, das alle Pythagoräischen Zahlentrippel bis zu einer vorgegebenen maximalen Hypothenusenlänge (sagen wir 100) berechnet und ausgibt.

Ein Pythagoräisches Zahlentrippel sind drei positive, ganze Zahlen a, b und c mit der Eigenschaft, dass

a2 + b2 = c2.

Dabei bezeichnet c die Hypothenuse im rechtwinkligen Dreieck. (Daher - nach dem Satz von Pythagoras - der Name Pythagoräische Zahlentrippel.)

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

8 Lösung(en)


import java.util.Scanner;

public class PythagoraeischeZahlentrippel {
  public static void main(String[] args) {
    new PythagoraeischeZahlentrippel().top();
  }

  void top() {
    int maxHypothenuse = einlesen("maximale Hypothenuse");
    schleife(5, maxHypothenuse);
  }

  void schleife(int minHyp, int maxHyp) {
    for(int hyp = minHyp; hyp <= maxHyp; hyp = hyp + 1) {
       alleTrippelZuHypothenuseAusgeben(hyp);   
    }  
  }

  double wurzelZweiHalbe = Math.sqrt(2.0) / 2.0;
  void alleTrippelZuHypothenuseAusgeben(int hyp) {
      int minA = (int) (hyp * wurzelZweiHalbe) + 1;
      for(int a = minA; a < hyp; a = a + 1) {
          testeTrippel(hyp, a);
      }
  }

  void testeTrippel(int c, int a) {
     double b = Math.sqrt(c * c - a * a);
     if(ganzzahlig(b)) {
         ausgabe(c, a, b);
     }
  }

  void ausgabe(int c, int a, double b) {
    System.out.println(c + ", " + a + ", " + (int)b);
  }

  double rechengenauigkeit = 0.00000000001;
  boolean ganzzahlig(double zahl) {
    return Math.abs(zahl - (int)zahl) < rechengenauigkeit;
  }

  Scanner sc = new Scanner(System.in);
  int einlesen(String frage) {
    System.out.println("Bitte geben Sie" + frage + " ein: "); // TODO Auto-generated method stub
    return sc.nextInt();
  }
  
} // end of class PythagoraeischeZahlentrippel
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

# @autor Philipp Gressly Freimann
# 7. April 2011
#
# Aufgabe http://www.programmieraufgaben.ch / http://www.programmieren-lernen.ch
# Finde alle Pythagoraeischen Zahlentrippel bis zu einer vorgegebenen maximalen Hypothenuse

def schleife(minHyp, maxHyp) 
  for hyp in minHyp..maxHyp
    alleTrippelZuHypothenuseAusgeben(hyp)
  end
end

WURZEL_ZWEI_HALBE = Math.sqrt(2.0) / 2

def alleTrippelZuHypothenuseAusgeben(hyp) 
  minA = (hyp * WURZEL_ZWEI_HALBE + 1).to_i
  for a in minA..(hyp-1)
    testeTrippel(hyp, a)
  end
end

def testeTrippel(c, a) 
  b = Math.sqrt(c * c - a * a)
  if(ganzzahlig(b)) 
    ausgabe(c, a, b)
  end
end

def ausgabe(c, a, b) 
  puts "#{c}, #{a}, #{b.to_i}"
end

RECHENGENAUIGKEIT = 0.00000000001
def ganzzahlig(zahl) 
  return RECHENGENAUIGKEIT > (zahl - zahl.to_i).abs
end

################
##### MAIN #####
################
print "Maximale Hypothenuse (>= 5):"
maxHypothenuse = STDIN.gets
schleife(5, maxHypothenuse.to_i)

##### END  #####
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

n = input("maximale Hypothenusenlaenge = ")
for c in range (1, n+1):
    for b in range (1, n+1):
        for a in range (1, n+1):
            if (a**2 + b**2 == c**2) and (c>b>a):
                print c, b, a
                

Lösung von: Name nicht veröffentlicht

/*
  PythagoraeischeZahlentrippel1

  @version 1.0
  @date 2011-09-03
  @author J. Mang
*/
public class PythagoraeischeZahlentrippel1{
  /**
  * Konstruktor
  */
  public PythagoraeischeZahlentrippel1(){

    // VERARBEITUNG und AUSGABE
    System.out.println();
    System.out.println(" ************************************");
    System.out.println(" *  PythagoraeischeZahlentrippel    *");
    System.out.println(" *                                  *");
    System.out.println(" ************************************\n");
    
    // EINGABE
    int hmax = 15; // Maximaler Hypothemusenwert
    
    System.out.println();
    System.out.println(" Hmax: " + hmax);
    System.out.println();

    // Berechnung mittels for-Schleifen
    for (int j = 1; j <= hmax; j++){

      for (int k = 1; k < j; k++){

        // Testausgabe
        //System.out.println("i = " +i+" j= "+j+" k= "+k);
        
        double h = Math.sqrt(j*j + k*k);
        
        // Testausgabe
        //System.out.println(h);

        if (h % 1.0 == 0.0 && h <= hmax){
          System.out.println(" "+(int)h+", "+j+", "+k);
        }
      }
    }
  }
  //main-Methode
  public static void main (String[] args){
  
    new PythagoraeischeZahlentrippel1();

    System.out.println();
    System.out.println(" Das Programm wurde beendet!");
  }
}
                

Lösung von: Jürgen Mang (Heinrich-Emanuel-Merck-Schule Darmstadt)

*process langlvl(saa2);
*process or('!');
*process limits(fixeddec(31));
*process aggregate, attributes;
*process flag(W), source, insource, options, xref, nest, number, offset;
*process gonumber, snap;

  /*****************************************************************\
 |* Autor : ph. gressly freimann (@ http://www.sanits-training.ch)  *|
 |* Datum : 30. Nov. 2011                                           *|
 |* Aufgabe w3tn-z38b (Programmieraufgaben.ch: Pytagohras)          *|
  \*****************************************************************/

 (SUBSCRIPTRANGE, SIZE, STRINGRANGE, STRINGSIZE):
 Pythago : proc(CMD_ARGS) options(main noexecops);

  %include Global;

  dcl CMD_ARGS char(255) varying;

  dcl start    bin fixed(63) value(5);

  dcl endStr   char(255)     varying;
  dcl end      bin fixed(63);

  endStr = liesProgrammArgument(CMD_ARGS, "e", "50");
  end    = endStr; /* Konvertiere nach Ganzzahl */

  call schleife(start, end);

  /* ENDE DER VERARBEITUNG */


   /***************\
  |* Unterprogramme |
   \***************/

  liesProgrammArgument: proc(CMD_ARGS, argumentName, default)
                        returns(char(255) varying);
    dcl argumentName char(255) varying;
    dcl CMD_ARGS     char(255) varying;
    dcl default      char(255) varying;
    dcl wert         char(255) varying init(default);

    dcl idx          bin fixed(15);
    idx = index(CMD_ARGS, "-" !! argumentName);
    if(0 = idx) then do;
      return("" !! wert);
    end; /* if */
    /* argumentName (inkl vorangehendes -) gefunden: */
    dcl end bin fixed(15);
    end = idx + length(argumentName) + 1;
    idx = index(CMD_ARGS, " ", end);
    /* letztes Argument? */
    if( idx <= end ) then do;
      idx = length(CMD_ARGS);
    end; /* if */
    dcl wertString char(255) varying;
    /* extrahiere Wert des Argumentes */
    wertString = substr(CMD_ARGS, end, idx - end + 1);

    wert = wertString;
    return(wert);
  end liesProgrammArgument;

  schleife: proc(cMin, cMax);
    dcl cMin bin fixed(63);
    dcl cMax bin fixed(63);

    dcl actC bin fixed(63);
    actC = cMin;
    do while(actC <= cMax);
      call pruefeHypothenuse(actC);
      actC = actC + 1;
    end; /* do while */
  end schleife;

  dcl wurzelAusZwei float(63)
      value(1.41421_35623_73095_04880_16887_24209_69807_85697);

  pruefeHypothenuse: proc(c);
    dcl c      bin fixed(63);

    dcl a      bin fixed(63);
    dcl aStart bin fixed(63);
    aStart = c * wurzelAusZwei / 2.0;
    aStart = aStart + 1;

    a = aStart;
    do while(a < c);
      call pruefeTrippel(c, a);
      a = a + 1;
    end; /* while */

  end pruefeHypothenuse;

  pruefeTrippel: proc(c, a);
    dcl c  bin fixed(63);
    dcl a  bin fixed(63);
    dcl b  bin fixed(63);

    b = sqrt(c*c - a*a);
    if(a*a + b*b = c*c) then do;
      call ausgabe(c, a, b);
    end; /* if */
  end pruefeTrippel;

  ausgabe: proc(c, a, b);
    dcl c bin fixed(63);
    dcl a bin fixed(63);
    dcl b bin fixed(63);
    put skip list("c=" !! c !! ", a=" !! a !! ", b=" !! b);
  end ausgabe;

 end Pythago;

                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

// Autor:				Andy Großhennig
// Solution for task:	Pythagoräische Zahlentrippel (Schleifen)

#include <iostream>

using namespace std;

void pythagoras()
{
	int iHypotenuseSquare = 0;
	int iAdjacentSquare = 0;
	int iOppositeSquare = 0;
	int iCount = 1;

	for(int iHypotenuse = 0;iHypotenuse <= 100;iHypotenuse++)
	{
		for(int iAdjacentSide = 0;iAdjacentSide < 100;iAdjacentSide++)
		{
			for(int iOppositeSide = 0;iOppositeSide < 100;iOppositeSide++)
			{
				iHypotenuseSquare = iHypotenuse * iHypotenuse;
				iAdjacentSquare = iAdjacentSide * iAdjacentSide;
				iOppositeSquare = iOppositeSide * iOppositeSide;

				if((iAdjacentSquare + iOppositeSquare) == iHypotenuseSquare)
				{
					printf("%i: %i^2 + %i^2 = %i^2\t\t%i + %i = %i\n",iCount, iAdjacentSide, iOppositeSide, iHypotenuse, iAdjacentSquare, iOppositeSquare, iHypotenuseSquare);
					iCount++;
				}
			}
		}
	}
}

int main()
{
	pythagoras();
	
	printf("\n\n");
	system("Pause");
	return 0;
}
                

Lösung von: Andy Großhennig (Bundeswehr)

function isPythagoreanTriple(a, b, c) {
   return (a * a + b * b === c * c);
}

var max = prompt("Maximale Hypothenusenlänge:");

for (var c = 1; c <= max; c++)
   for (var b = 1; b <= max; b++)
      for (var a = 1; a <= max; a++)
         if (a < b && isPythagoreanTriple(a, b, c))
            console.log(a + " | " + b + " | " + c);
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// C++ 20
#include <iostream>
#include <vector>
using v_t = std::vector<std::tuple<int, int, int>>;

auto get_pythagorean_triple(int max) -> v_t {
    v_t v{};
    for (auto m{ 2 }, n{ 1 }; m * m < max; (n += 2) > m ? (n = ++m % 2 ? 2 : 1) : int()) {
        const auto  a{ m * m - n * n },
                    b{ 2 * m * n },
                    c{ m * m + n * n };
        for (auto i{ 1 }; i * c <= max; ++i)
            v.push_back({ i * c, i * b, i * a });
    }
    return v;
}

int main() {
    for (const auto& [c, b, a] : get_pythagorean_triple(100)) // C++ 20!
        std::cout << c << "^2 = " << b << "^2 + " << a << "^2" << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

5, 4, 3
10, 8, 6
13, 12, 5
15, 12, 9
17, 15, 8
20, 16, 12
25, 20, 15
25, 24, 7
26, 24, 10

...

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit:
Schwierigkeit: k.A.
Webcode: w3tn-z38b
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen