/* A test case that with empty input. */ static void empty(void **state) { (void) state; /* unused */ assert_true(isHappy(1)); assert_true(isHappy(989)); assert_false(isHappy(4)); assert_false(isHappy(111)); }
int isHappy(int n) { int count = 0, i = 0; int numbers[20] = {0}; hashSum[n] = 1; if (n == 1) { return 1; } while (1) { numbers[count++] = (n % 10); n /= 10; if (n == 0) { break; } } int sum = 0; for (i = 0; i < 20; i++) { sum += numbers[i] * numbers[i]; } if (hashSum[sum] == 1) { return 0; } else { return isHappy(sum); } return 0; }
//awesome recursive solution bool isHappy(int n) { int nextN = int2BitNum(n); if (nextN == 1) return true; else if (nextN <= 9) return false; return isHappy(nextN); }
void main() { int n = 1; printf("2 : %d\n", isHappy(2)); /* for(n = 1; n < 11; n++) { printf("%d:%d\n", n, isHappy(n)); } */ }
int main(void) { int n = 19; if(isHappy((n))) printf("%d is a happy number",n); else printf("%d is not a happy number",n); return 0; }
bool isHappy(int n) { if (n==1) return true; if (n==4) return false; int num=0; while (n) { int t=n%10; num+=t*t; n/=10; } return isHappy(num); }
bool isHappy(int n, unordered_set<int> seen) { if (n == 1) return true; else if (seen.count(n)) return false; seen.insert(n); int new_n = 0; while (n) { new_n += (n % 10) * (n % 10); n /= 10; } return isHappy(new_n, seen); }
void main() { int number; printf("pls input the number: "); scanf("%d", &number); if(isHappy(number)) printf("%d is a happy number.\n", number); else printf("%d is not a happy number.\n", number); }
bool isHappy(int n, unordered_set<int> &hs) { if (n == 1) { return true; } if (hs.find(n) != hs.end()) { return false; } hs.insert(n); string s = to_string(n); int v = 0; for (auto c: s) { v += (c-'0') * (c-'0'); } return isHappy(v, hs); }
bool isHappy(int n) { if (n<0) return isHappy(-n); set<int> myset; while (n!=1) { if (myset.find(n)!=myset.end()) return false; else { myset.insert(n); n = sumDigits(n); } } return true; }
bool isHappy(int n) { if (!n) return false; int result = 0; while (n) { int rem = n % 10; result += rem * rem; n /= 10; } if (result == 1) return true; if (result < 10) return false; return isHappy(result); }
int isHappy(int n) { int lastOne = 0, nextNum = 0; if (n == 1) return 1; else if (n == 0 || n == 2 || n == 3) return 0; else { while (n) { lastOne = n % 10; nextNum += lastOne * lastOne; n = n / 10; } printf("%d\n", nextNum); return isHappy(nextNum); } }
bool isHappy(int n) { int sum=0; if(n<1)return false; while(n!=0){ sum=(int)pow(n%10,2)+sum; n=n/10; } //cout<<sum<<endl; if (1==sum){ return true; } if (find(vec.begin(),vec.end(),sum)!=vec.end()) return false; else{ vec.push_back(sum); return(isHappy(sum)); } }
bool isHappy(int n) { vector<int> digit; int rest; int newvalue; bool result; if(n<10) { if(n==1 || n==7) return true; else return false; } else { rest=n; newvalue=0; while(rest>0) { digit.insert(digit.begin(),rest%10); rest=rest/10; } for(int i=0;i<digit.size();i++) newvalue+=(int)pow(digit[i],2); result=isHappy(newvalue); //To check the return value of the recursive function. To return the value. if(result) return true; else return false; } }
bool isHappy(int n) { puts("enter"); if(n <= 0) return false; else if(n == 1) return true; int i = 0, m = 0, c= 0, j = 0; puts("before while"); while(n) { puts("enter while"); c = n % 10; n /= 10; for(j = 1; j < i+1; j++) j *= 10; m += (c * c) * j; i++; } printf("n : %d\n", m); return isHappy(m); }
int main(void) { //Variable declerations int starttime; unsigned int i; unsigned int happycount; unsigned int checknum; //Variable initialisation happycount = 0; starttime = time(NULL); //Storing the starttime ensures that the results for a given seed are independent of system load. //(i.e. if the time displayed during an execution is sefined as SEED the same results will be displayed on the same computer.) //Input check and error handling if (starttime==-1) { fprintf(stderr,"Error:Could not retrieve the calendar time. Stopping.\n"); return EXIT_FAILURE; } srand((unsigned int)SEED); printf("Current time is %d\n\n",SEED); for (i=1;i<=COMPUTATIONS;i++) { checknum=((rand()%32768)+1)*((rand()%32768)+1)+1; //Checking whether or not a number is happy before checking if it's prime makes the program about 8 times faster than the reverse. //(If you don't believe me, try it out yourself ;) ) if (isHappy(checknum)) { if (isPrime(checknum)) { happycount++; printf("%u is happy prime\n",checknum); } } } printf("\nFound %5.2f%% happy prime numbers\n",((float)happycount/COMPUTATIONS)*100); return EXIT_SUCCESS; }
bool isHappy(int n) { if(n<=0) return 0; else if(n<=9) { if(n==1 || n==7) return 1; else return 0; } else { int a=0,b=0; while(n!=0) { b=n%10; a+=b*b; n=n/10; } return isHappy(a); } }
int main() { printf("res: %d\n", isHappy(19)); printf("res: %d\n", isHappy(102)); return 0; }
TEST(HappyNumberTestCase, Normal) { EXPECT_TRUE( isHappy(19) ); }
int main(int argc, const char * argv[]) { // insert code here... printf("%d\n", isHappy(1)); return 0; }
bool happyNumber(int n) { return isHappy(n, unordered_set<int>()); }
int main() { printf_s("The result is: %s\n", isHappy(10001) ? "TRUE" : "FLASE"); return 0; }
int main() { //happy number isHappy(4); }
int main() { isHappy(99); return 0; }
bool isHappy(int n) { unordered_set<int> hs; return isHappy(n, hs); }