square and triangular numbers problem

I am trying to write a program to determine whether the input integer N is a square number and/or a triangular number.


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>
#include <cmath>
using namespace std;
int main (){
    long long int n;
    cin>>n;
    if(n==1){
        cout<<"Both";
        return 0;
    }else if((sqrt(8*n+1)!=floor(sqrt(8*n+1)))&&(sqrt(n)!=floor(sqrt(n)))){
        cout<<"Neither";
        return 0;
    }else if((sqrt(8*n+1)==floor(sqrt(8*n+1)))&&(sqrt(n)!=floor(sqrt(n)))){
        cout<<"Triangular";
        return 0;
    }else if((sqrt(8*n+1)!=floor(sqrt(8*n+1)))&&(sqrt(n)==floor(sqrt(n)))){
        cout<<"Square";
        return 0;        
    }else if((sqrt(8*n+1)==floor(sqrt(8*n+1)))&&(sqrt(n)==floor(sqrt(n)))){
        cout<<"Both";
        return 0;
    }
}


Thanks a lot!
@kryptomega,
You don't actually state what your problem is!

You do, however, have a huge amount of repetition and you are comparing floating-point numbers for exact equality, which is always dangerous, due to the finite accuracy with which they can be stored. Although I haven't done that below, the test for a triangle number is equivalent to whether 8n+1 is a square number (as you are clearly intending in your code).

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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

using INT = unsigned long long;

//======================================================================

bool isSquare( INT N )
{
   INT r = sqrt( 0.5 + N );                 // Deliberate use of truncation
   return r * r == N;
}

//======================================================================

bool isTriangular( INT N )
{
   INT r = 0.5 * sqrt( 1.0 + 8.0 * N );     // Deliberate use of truncation
   return r * ( r + 1 ) / 2 == N;
}

//======================================================================

int main()
{
   string result[] = { "neither", "square", "triangular", "both" };
   for ( INT i = 1; i <= 100; i++ ) cout << i << ": " << result[ isSquare(i) + 2 * isTriangular(i) ] << '\n';
}
Last edited on
@lastchance,
Thanks a million!

My code updated as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

bool isSquare( long long int N ){
   long long int r = sqrt( 0.01 + N );                
   return r * r == N;
}
bool isTriangular( long long int N ){
   long long int r = 0.5 * sqrt( 1.0 + 8.0 * N ); 
   return r * ( r + 1 ) / 2 == N;
}
int main(){
    long long int i;
    cin>>i;
   string result[3] = { "Neither", "Square", "Triangular", "Both" };
    cout <<result[ isSquare(i) + 2 * isTriangular(i) ] << '\n';
}


But turned out with error "too many initializers for ‘std::string [3] {ak std::basic_string<char> [3]}’
@string result[3] = { "Neither", "Square", "Triangular", "Both" };


What's my problem?
Thanks!


P.S.
Sample input and output for your reference:

e.g. 1
Input: 10
Output: Triangular
e.g. 2
Input: 25
Output: Square
e.g. 3
Input: 37
Output: Neither
e.g. 4
Input: 36
Output: Both
On line 17 you declared the array to have 3 elements ... and then gave it 4.
Oh, I see, this problem has frustrated me a lot...
Thanks a trillion!
Topic archived. No new replies allowed.