Tags:
create new tag
view all tags
/*
    Programma per trovare le terne pitagoriche composte da interi inferiori o uguali
    a un certo valore. Le terne pitagoriche sono terne di interi per le quali risulta
    
    a^2 = b^2 + c^2
    
    Il programma richiede la libreria matematica, che di default non viene linkata.
    Quindi, la riga di comando per il compilatore deve essere del tipo

    gcc terne.c -o terne -lm

    con l'opzione -lm in fondo, che segue tutte le altre.
*/

#include "stdio.h"
#include "math.h"

main () {

  int x,y,max;
  double z,y_max;

  int contatore=0;

  printf ("Inserire il limite :  ");
  scanf ("%d",&max);

  for (x=1; x<=max; x++) {
      /* è leggermente più efficiente calcolare la sqrt solo una volta per ogni x */
      y_max=sqrt(max*max - x*x); 
      for (y=x; y<=y_max; y++) {
          z = sqrt(x*x + y*y);
          if (!(floor(z)<z))
              printf ("terna pitagorica %d : %d %d %d \n", ++contatore, x, y, (int)z);
      }
  }
  printf("numero terne : %d\n", contatore);
}
-- ArcangeloTripi - 18 Nov 2002
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2003-10-04 - AndreaSterbini






 
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback