Mathematics - Lagranges statement

This is a discussion on Mathematics - Lagranges statement within the C++ Programming forums, part of the General Programming Boards category; I want to make a program that showes "Lagranges - statement": Every even number can be made up by at ...

  1. #1
    Unregistered
    Guest

    Mathematics - Lagranges statement

    I want to make a program that showes "Lagranges - statement":
    Every even number can be made up by at least one combination
    of four or less square numbers.

    In the program you shall enter a specific, even number (haven't added that to the code yet). Then the program shall test all the possible combinations with 1,2,3 or 4 square numbers in (will be alot of them), and then subtract them with the specific number.
    If it returns 0, then the combinations that went out should be outputted to the screen. (The screen outputs which I've used now is just for seeing if it works).

    This is what I've got:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {

    int i;
    int x;

    int a;
    int b;
    int c;
    int d;
    int e;
    int f;

    for (i=0; i<=6; i++)
    {
    if(i==1){a=i*i;}
    if(i==2){b=i*i;}
    if(i==3){c=i*i;}
    if(i==4){d=i*i;}
    if(i==5){e=i*i;}
    if(i==6){f=i*i;}
    }

    std::cout << a << " ";

    for (i=0; i<=6; i++)
    {
    if (i==1)
    {std::cout << a*2 << a*3 << a*4 << a*5 << a*6 << "\n";}

    if (i==2)
    {std::cout << a+(b*1) << a+(b*2) << a+(b*3) << a+(b*4) << a+(b*5) << a+(b*6) << "\n";}

    if (i==3)
    {std::cout << a+(c*1) << a+(c*2) << a+(c*3) << a+ (c*4) << a+(c*5) << a+(c*6) << "\n";}

    if (i==4)
    {std::cout << a+(d*1) << a+(d*2) << a+(d*3) << a+(d*4) << a+(d*5) << a+(d*6) << "\n";}

    if (i==5)
    {std::cout << a+(e*1) << a+(e*2) << a+(e*3) << a+(e*4) << a+(e*5) << a+(e*6) << "\n";}

    if (i==6)
    {std::cout << a+(f*1) << a+(f*2) << a+(f*3) << a+(f*4) << a+(f*5) << a+(f*6) << "\n";}

    system("PAUSE");

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    (To keep the combinations down you can make the program not to use numbers greater than the specific number and not smaller than the number divided by 4)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    int lagranges(int num /* in */, int rtn[4] /* out */)
    {
    int x, y, z;

    if(num < 0) return -1;

    //initialize rtn array
    for(x = 0; x < 4; x++) rtn[x] = 0;

    x = 0;

    while(num)
    {
    for(y = num; num > 0 && x < 4; y--)
    {
    z = (int)sqrt(y);
    if(z * z == y)
    {
    rtn[x] = z;
    x++;
    num -= y;
    y = num + 1;
    }
    }
    if(x == 4) break;
    }

    return num;
    }
    Last edited by sigma; 03-29-2002 at 03:52 PM.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    thanx man! U really good.

    Although, please complete the whole program for me. I just can't call that function.

    thanx al0t!

  5. #5
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    Although, please complete the whole program for me. I just can't call that function.
    I got the bulk of the logic done, so you should be EASY for anyone to implement it in a program.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Ok, you're right. Shouldn't be letting everyone else make your programs, otherwise, you'll never learn.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    I tried the function but it doesn't seem to work. Can you help me?

  8. #8
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    I edited the algorithm I posted before and added one line to it.
    It works for most cases, but will need to be modified to work for every case.

  9. #9
    Unregistered
    Guest

    Smile

    Thanks. That thing I posted before about keeping combinations down by only using square numbers not smaller than the specific number divided by 4 isn't right. What is right is that at least one square number has to be bigger than the specific number divided by 4.

    I have no idea about how fast computers can compute all the combinations (depends of Mhz of course), but here's a table of the first 50 square numbers:

    <script language="JavaScript">

    for (x=0; x<=50; x++)
    {
    document.write("<table border=1><td>"+x+"</td><td>"+x*x+"</td></table>");
    }
    </script>

    Just so you know how many there are.

  10. #10
    Unregistered
    Guest
    Please, anyone, I really want to complete this program! Just help me because I'm not that good at the programming part.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int integer, integer_sqrt;
          bool disabled;
    
          std::cout << "Eneter an integer: ";
          std::cin >> integer;
          std::cout << "\n--------------------------------------------------------------------------------Possible addings:\n--------------------------------------------------------------------------------\n";
    
          integer_sqrt = sqrt(integer);
          int Sqrt_numbers[integer_sqrt];
    
          for(int i=0; i<=integer_sqrt; i++)
          {
          Sqrt_numbers[i] = i*i; Sqrt_numbers[i] == !disabled;
          }
    
          for(int x=1; x<=sizeof Sqrt_numbers/4; x++)
          {
    
             for(int y=1; y<=sizeof Sqrt_numbers/4; y++)
             {
    
             if(Sqrt_numbers[x]+Sqrt_numbers[y] == integer)
             {std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << "\n";}
    
                for(int z=1; z<=sizeof Sqrt_numbers/4; z++)
                {
    
                if(Sqrt_numbers[x]+Sqrt_numbers[y]+Sqrt_numbers[z] == integer)
                {std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << " + " << Sqrt_numbers[z] << "\n";}
    
                   for(int i=1; i<=sizeof Sqrt_numbers/4; i++)
                   {
    
                   if(Sqrt_numbers[x]+Sqrt_numbers[y]+Sqrt_numbers[z]+Sqrt_numbers[i] == integer)
                   {std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << " + " << Sqrt_numbers[z] << " + " << Sqrt_numbers[i] << "\n";}
    
                   }
    
                }
    
             }
    
          }
    
          system("PAUSE");
          return 0;
    }
    Lagranges statement: if we disregard 1, all integers can be summarized by two, three or four
    sqaure numbers. You will never have to use five or six.

    Well, I decided to give this another try, now that I feel more experiened regarding C++.
    There is now only one major issue left, which I have unsuccesfully tried to solve myself:

    The program writes all of the possible summerizes for the integer, it's just that I sometimes
    get the combinations more than one time. Example: type in "9" in the program, and the possible
    summerizes of "1+4+4, 4+1+4, 4+4+1" will be displayed.

    This is because the program adds the first number and on in the array, with the rest of the
    numbers in the array. For example, the array looks like this: "1, 4, 9", then the program
    will add "1+1, 1+4, 1+9, 4+1, 4+4, 4+9, 9+1, 9+4, 9+9". Each time the combination equals
    the specific number, the combination is displayed. If the specific number is "5", it would
    have been equaled by both 1+4 and 4+1, and thus displayed.

    I have tried using a boolean value, to disable the specific combination. Like:

    if(Sqrt_numbers[x]+Sqrt_numbers[y] == integer && Sqrt_numbers[x]+Sqrt_numbers[y] == !disabled)
    {std::cout << Sqrt_numbers[x]+Sqrt_numbers[y]; Sqrt_numbers[x]+Sqrt_numbers[y] == disabled;}

    In the loop where the array is initialized, I have also tried to give all of the numbers in it
    the value of !disabled, as any number except 0 would get the value of true, and thus disabled.

    Anyway, my purpose with this is that the current combination, for example: x=1 and y=4, are
    together disabled, so that they won't be displayed again when x=4 and y=1. Unfortunately, this
    doesn't work.

    Any ideas?

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Here's the program, compiled and zipped, as well. It's sad you can't upload something, once you've posted, when you want to edit later.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Please, how can I make combinations of numbers not to appear more than once?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21