HELP

Feb 21, 2019 at 2:05pm
How WOULD I CHANGE THIS CODE SO THAT THE PROGRAM FINDS THE SMALLEST INTEGER THAT CAN BE EXPRESSED AS THE SUM OF SQUARES OF TWO DIFFERENT PATHS?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;


void sumSquare(int n)
{
    int i,j =1;
    for (i = 1; i * i <= n; i++)
        for ( j = 1; j * j <= n; j++)
            if (i * i + j * j == n) {
                cout << i << "^2 + "
                << j << "^2 = " << n<< endl;
            }
}


int main()
{


    int n = 50;
    sumSquare(n);
}
Feb 21, 2019 at 2:10pm
You need to find the smallest integer that can be expressed as the sum of square of two numbers, okay, where are the two numbers?

Get number 'A'.
Get number 'B'.

Required = A*A + B*B.
Yes?


To find the two smallest numbers whose square equals the given number, you can something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sumSquare(int n)
{
    int lowesti, lowestj;
    bool is_found = false;
    int i, j = 1;

    for (i = 1; i * i <= n; i++)
        for (j = 1; j * j <= n; j++)
            if (i * i + j * j == n) {
                if (!is_found) {
                    lowesti = i;
                    lowestj = j;
                    is_found = true;
                }
                else {
                    if (i + j < lowesti + lowestj) {
                        lowesti = i;
                        lowestj = j;
                    }
                }
            }
}
Last edited on Feb 21, 2019 at 2:19pm
Feb 21, 2019 at 2:13pm
the program should find the smallest number n= A*A + B*B= C*C + D*D
Feb 21, 2019 at 2:22pm
Ah okay.

So do the same thing but check for lowesti + lowestj == i+j later (once you've found lowesti and lowestj). And if nothing is found then do the first loop again but make sure you don't take the same values for lowesti and lowestj.

Donno if there's a better way.
Last edited on Feb 21, 2019 at 2:26pm
Feb 21, 2019 at 2:26pm
can you show me?
Feb 21, 2019 at 3:01pm
Give it a try
Feb 21, 2019 at 3:29pm
Loop a number, n, from a small number, either until you find a match, or until a sufficiently large number.

Change sumSquare to return a bool, and have it keep track of the number of times the pattern you're looking for (A*A + B*B == n) is found. If it's found twice, and (A*A != C*C and A*A != D*D), then return true. Otherwise, if you've exhausted all the choices, return false and try the next highest n.
Feb 21, 2019 at 3:36pm
HELP isn't a topic title.
http://www.catb.org/esr/faqs/smart-questions.html#bespecific

Running around screaming in ALL CAPS isn't helpful either.
http://www.catb.org/esr/faqs/smart-questions.html#writewell

Feb 21, 2019 at 4:44pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
   set<int> S;
   int d = 0, n = 10000000;
   bool best = false;

   while( !best )                 // Seek smallest n with c <= d and c^2+d^2 seen before
   {
      best = true;
      for ( ++d; best && d * d < n; ++d )
      {
         int dd = d * d;
         int ccmax = min( n - 1 - dd, dd );      // Seek to reduce current n with c <= d
         for ( int c = 1; best && c * c <= ccmax; c++ )
         {
            int test = dd + c * c;
            if ( !( S.insert( test ).second ) )  // Seen before
            {
               n = test;
               best = false;                     // Get out of c and d loops
            }
         }
      }
   }

   cout << n;
}



or even

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   for ( int n = 2; ; n++ )
   {
      bool found = false;
      int mid = sqrt( n / 2.0 );
      for ( int d = mid; d * d < n; ++d )
      {
         for ( int c = 1; c <= mid ; c++ )
         {
            if ( c * c + d * d == n )
            {
               if ( found )
               {
                  cout << n;
                  return 0;
               }
               found = true;
            }
         }
      }
   }
}
Last edited on Feb 21, 2019 at 5:32pm
Topic archived. No new replies allowed.