uloha:pythagorejske_trojuhelniky

Zadání

Pro svoje spolužáky chcete vytvořit příklady na řešení trojuhelníku. Všimli jste si, že se nejsnáze řeší trojúhelníky s celočíselnými stranami (takovým se říká https://cs.wikipedia.org/wiki/Pythagorejsk%C3%A1_trojice Pythagoreské).

Vygenerujte tedy co nevíce takových trojuhelníků, jejichž strany jsou menší než 100.

Možnosti

  • náhodné generování
  • systematické generování
    1. iterací (for cykly)
    2. rekurzivně (funkce volá funkci)

Řešení

Iterativní systematické generování

    function isRound(number){
    	return Math.round(number) == number
    }
 
    function solvePythagorean(a, b){
    	return Math.sqrt( a*a + b*b )
    }
 
    function isPythagorean(a, b){
      var c = solvePythagorean(a, b)
      return isRound(a) && isRound(b) && isRound(c)
    }
 
    for(var x=1;x<=100;x++){
     for(var y=1;y<=100;y++){
      if(isPythagorean(x,y)){
      console.log("a="+x+",b="+y +",c="+ solvePythagorean(x,y))
      }
     }
    }
  • uloha/pythagorejske_trojuhelniky.txt
  • Poslední úprava: 2023/11/15 20:54
  • autor: 127.0.0.1