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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
using namespace std;
vector<int> squares;
int square_pair(int target)
{
int low = 0 , high = floor(sqrt(target)) , count = 0 , sqlow = squares[low] , sqhigh = squares[high];
while( low <= high )
{
if ( pow(sqlow,2) + pow(sqhigh,2) < target )
{
low++;
}
if ( pow(sqlow,2) + pow(sqhigh,2) > target )
{
high--;
}
if ( pow(sqlow,2) + pow(sqhigh,2) == target )
{
count++;
}
}
}
int main()
{
int t , x , *nums , max = 0 , temp;
ifstream input("test.txt");
input >> t; //t is the number of numbers that have to be added to nums[]
nums = new int[t];
for( int i = 0 ; i < t ; i++)
{
input >> temp;
nums[i] = temp;
if( temp > max )
{
max = temp;
}
}
input.close();
int maxroot = (int) floor(sqrt(max));
for ( int i = 0; i <= maxroot; i++ )
{
x = i * i;
squares.push_back(x);
}
for ( int i = 0; i < t; i++)
{
int sq = square_pair(nums[i]);
cout << sq << endl;
}
return 0;
}
|