Thread: Pythagorans theorem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    Pythagorans theorem

    back back back to the days of nineth grade geometry, when you may have been learning about advcanced Pythagorans theorem. Remember when you couldn't just give the square root of the hypotenuse(a^2 + b^2 = c^2 and then sqrt(c^2) is the hypotenuse) because it "wasn't exact". Well, some of you may have had to simplify the hypotenuse into the simplest root form, like a triangle with sides of 4, leaving you with 16 + 16 =32, but having to simplify 32 into "root 2*16| root 2 * root 16 | or 2root4. now, if this still makes sence and by some merical I can type coherently this late, does anyone have any ideas, or thoughts on making this into a program? I have everything up until I have to simplify 32 into 2 root 4. I was thinking maybe trying to devide ever perfect square into 32 that's <32, but I'm sure there's an easyer way. if anyone can help, I'd appreciate it alot, thanks.


    the code I have so far:

    #include <iostream>
    #include <math.h>
    using namespace std;
    int main()
    {
    double
    cout << "1 for hypotenuse, 2 for a leg";
    cin >>h;
    if (h == 1)
    {
    cout <<"\nenter the length of one leg, then the other.";
    cin >>l1>>l2;
    a = pow(l1, 2);
    b = pow(l2, 2);
    c = a + b;
    d = sqrt(c);
    cout <<"\nhypotenuse = "<<c<<" or rooted, "<<d<<endl;

    ya... ya... not much, but here's a program I worked out that kinda works, may be usefll for those studying the topic:



    #include <iostream>
    #include <math.h>
    #include <conio.h>
    using namespace std;
    int main()
    {
    double a, b, c, d, e, f, z;
    int i;
    cout << "This program is to be used to figure out the side of a right triangle using Pythagoran's Theorum. if you want to know the background of pythagoran's theorum, type 3"<<endl;
    for (;;)
    {
    cout << "To find out the length of the hypotenuse, press 1, and to find the length of a leg, press 2, to check to see if a triangle is right, press 3, for help, type 5 and to exit, press 9."<<endl;
    cin >>i;
    switch (i)
    {
    case 5:
    cout << "basicly Pythags Theorum is used tyo find the length of the hypotenuse, or leg of a right triangle, and only a right triangle. Incidently, since it;s only used to find out a right triangle, you can use it to find out if the triangle in question is square or not if you have the length of the two sides. The theorum finds out the length of a side using the fromula a^2 + b^3 = c^2(for the hypotenuse), or c^3 - b^2 = a^2. Then after you have the answer, you square root it to get the side. This program does this very fast and easily.\n\n\n"<<endl;
    break;
    case 9:
    return 0;

    case 1:
    cout << "Please enter the length of one side, then the other."<<endl;
    cin >>a>>b;
    c = pow(a, 2);
    d = pow(b, 2);
    e = c + d;
    f = sqrt(e);
    cout << "the length of the hypotenuse is " <<f<<"\n\n\n";
    break;
    case 2:
    cout << "Please enter the length of one side, then the hypotenuse "<<endl;
    cin >>a>>b;
    c = pow(a, 2);
    d = pow(b, 2);
    e = d - c;
    f = sqrt(e);
    cout << "the length of the missing side is " <<f<<"\n\n\n";
    break;
    case 3:
    cout << "please enter the length of two sides " <<endl;
    cin >>a>>b;
    c = pow(a, 2);
    d = pow(b, 2);
    e = d + c;
    f = sqrt(e);
    cout << "\nNow, please enter the length of the hypotenuse ";
    cin >>z;
    if (z == f)
    cout <<"yes, the triangle is a right triangle\n\n\n";
    else if (z < f)
    cout << "Nope, you've got yourslef an acute triangle\n\n\n";
    else if (z > f)
    cout << "Nope, you've got yourself an obtuse triangle\n\n\n";
    break;
    default:
    cout <<"please enter a valid entry"<<endl;
    }
    }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Taking your result of 32, you need to extract all the integer divisors (or factors?), such that x*y = 32

    Then you look to see which x,y pairs are capable of being written as numbers
    - like x=16 has an integral root of 4, and can therefore be written as 4)
    - y=2, which has no integral root, and must be written as root2

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    * +++Date last modified: 05-Jul-1997 */
    
    /*
    ** factor.c -- print prime factorization of a number
    ** Ray Gardner -- 1985 -- public domain
    ** Modified Feb. 1989 by Thad Smith > public domain
    **
    ** This version takes numbers up to the limits of double precision.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int prevfact = 0;
    void factor (double);
    void show (double, int);
    
    main (int argc, char *argv[])
    {
          while ( --argc )
                factor(atof(*++argv));
          return 0;
    }
    
    void factor (double n)
    {
          double d;
          int k;
    
          prevfact = 0;
    
          d = n+1;     /* test for roundoff error */
          if (n+3 != d+2)
          {
                printf("%0.0f is too large to process.\n", n);
                return;
          }
          if (fmod(n,1.) != 0.0)
          {
                printf("%f is not an integer.\n",n);
                return;
          }
          printf("%0.0f  ",n);
          if ( n < 2. )
          {
                printf("is less than 2.\n");
                return;
          }
          else if ( n > 2. )
          {
                d = 2;
                for ( k = 0; fmod(n,d) == 0.0; k++ )
                      n /= d;
                if ( k )
                      show(d,k);
                for ( d = 3; d * d <= n; d += 2 )
                {
                      for ( k = 0; fmod(n,d) == 0.0; k++ )
                            n /= d;
                      if ( k )
                            show(d,k);
                }
          }
          if ( n > 1 )
          {
                if ( ! prevfact )
                      printf(" is prime");
                else  show(n,1);
          }
          printf("\n");
    }
    
    void show (double d, int k)
    {
          if ( prevfact )
                printf(" * ");
          else  printf(" = ");
          prevfact++;
          printf("%0.0f",d);
          if ( k > 1 )
                printf("^%d",k);
    }
    this is a factoring program from a snippets zip file i got somewhere. i'm too tired to figure it out, but you might be able to find something in it. hope it helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Riddle Thread
    By Snafuist in forum A Brief History of Cprogramming.com
    Replies: 108
    Last Post: 03-30-2009, 11:07 AM
  2. Pythagorean Theorem Program Comment, criticize, or whatever
    By Sshakey6791 in forum C++ Programming
    Replies: 7
    Last Post: 02-13-2009, 02:48 AM
  3. Separating Axis Theorem
    By Kenki in forum Game Programming
    Replies: 20
    Last Post: 05-22-2005, 11:49 AM
  4. Galois Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-06-2002, 06:20 PM
  5. David's CProgramming Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-14-2001, 12:29 AM