0 Replies - 1773 Views - Last Post: 28 September 2012 - 12:18 PM

#1 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Test for square and triangular numbers

Posted 28 September 2012 - 12:18 PM

Description: You will need to include "math.h" and link the math library (for sqrt). Compiles using g++ and Visual C++.IsSquare returns true if the value passed to it is a square number. IsTriangle returns true if the value passed to it is triangular.
bool IsSquare(int n) {
	double nsq = sqrt((double)n);
	nsq -= (int)nsq;
	return (nsq == (double)0.0);
}

bool IsTriangle(int n) {
	return (IsSquare(8*n+1));
}

Is This A Good Question/Topic? 0
  • +

Page 1 of 1