Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,493 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,338 people online right now. Registration is fast and FREE... Join Now!




four hoboes riddle . need employ square root function

 
Reply to this topicStart new topic

four hoboes riddle . need employ square root function, geezer in Arkansas needs assistance

bollbudb
23 Feb, 2007 - 06:10 PM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2007
Posts: 3


My Contributions
CODE
#include <iostream>
using namespace std;

void main()
{
  int i, j, k, l;           // Counter variables

  //-------------------- description object -----------------------------------
  // Tell user what program does
  cout << "The following numbers indicate valid solutions to the lazy hobo\n";
  cout << "riddle.\n" << endl;
  //-------------------- end description object -------------------------------

  //-------------------- computation object -----------------------------------
  // Determine valid solutions to i*i + j*j + k*k + l*l = 200
  for (i=1; i<=14; i++) {
    for (j=1; j<=14; j++) {
      for (k=1; k<=14; k++) {
        for (l=1; l<=14; l++) {
          if (i*i + j*j + k*k + l*l == 200) {
            // Output valid number sequence
            cout << i << " " << j << " " << k << " " << l << endl;
          }
        }
      }
    }
  }
  //-------------------- end computation object -------------------------------

} //end main

User is offlineProfile CardPM
+Quote Post

jstephens
RE: Four Hoboes Riddle . Need Employ Square Root Function
23 Feb, 2007 - 07:59 PM
Post #2

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 192



Thanked: 1 times
My Contributions
So what is your question? Is there a specific part of this code that is not working?
User is offlineProfile CardPM
+Quote Post

sidharth
RE: Four Hoboes Riddle . Need Employ Square Root Function
23 Feb, 2007 - 08:08 PM
Post #3

New D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 38


Dream Kudos: 550
My Contributions
what do u want ??
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Four Hoboes Riddle . Need Employ Square Root Function
23 Feb, 2007 - 10:19 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well not exatly sure what your question is. As I recall from number theory (and I could be wrong here, but looking at you code I don't think so) the question you are trying to solve with this code is: how many ways can you write 200 as the sum of 4 unique squares.

The problem with the code above is that it will find duplicate values since it tries each of number beween 1 and 14 twice.

The problem can be solved with simple combinatorics, or you can write an algorithm to search of an answer.

Try doing the problem on paper first. What you are looking for is a systematic approch to the problem.

to get your code to work, find a way to keep the programming from duplicating searches... ie 1*1 + 2*2 + 3*3 + 4*4 == 3*3 + 1*1 + 2*2 +4*4.. so we only want to check each once... think of it in terms of combinations... how do you select all the combinations of 4 elements in a set... once you have a set of 4, check to see if it is an answer.

This post has been edited by NickDMax: 24 Feb, 2007 - 10:12 AM
User is offlineProfile CardPM
+Quote Post

sidharth
RE: Four Hoboes Riddle . Need Employ Square Root Function
24 Feb, 2007 - 01:13 AM
Post #5

New D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 38


Dream Kudos: 550
My Contributions
to employ nick's idea make changes to loops how : put l=k , k= j , j =i in place of zeros
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month