static bool isHappy (int n) {
if(n<=0) return false;
Set<int> previouslySeen;
while (n>1 && !previousySeen.contains(n)) {
previouslySeen.add(n);
n = completeSumOfSquaredDigits(n);
}
return n==1;
}
static bool isHappy(int n) {
if(n<=0) return false;
Set<int> endpoints;
endpoints += 1,4,16,37,58,89,145,42,20; // comma operator makes endpoints all the numbers and only
//these numbers
while(!endpoints.contains(n)) {
n=computeSumOfSquaredDigits(n);
}
return n==1;
}
Let you define data structures without being all hardcore classes/object orientated programming
Exactly the same as classes except for default privacy (struct has default of public)
struct fraction {
int numerator;
int denominator;
}
bool operator < (const fraction& one, const fraction & two) {
return one.numerator*two.denominator < one.denominator * two.numerator;
}