Skip to content

Instantly share code, notes, and snippets.

@aire-con-gas
Last active December 5, 2018 16:44
Show Gist options
  • Save aire-con-gas/54baa62a8e4ecdb821061c2b64f63cf3 to your computer and use it in GitHub Desktop.
Save aire-con-gas/54baa62a8e4ecdb821061c2b64f63cf3 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/kiboxat
/*
public boolean isHappy(int n) {
int x = n;
int y = n;
while(x>1){
x = cal(x) ;
if(x==1) return true ;
y = cal(cal(y));
if(y==1) return true ;
if(x==y) return false;
}
return true ;
}
public int cal(int n){
int x = n;
int s = 0;
while(x>0){
s = s+(x%10)*(x%10);
x = x/10;
}
return s ;
}
*/
'use strict';
function isHappy(n) {
var x = n;
var y = n;
while (x > 1) {
x = cal(x);
console.log('x', x);
if (x === 1) {
return true;
}
y = cal(cal(y));
console.log('y', y);
if (y === 1) {
return true;
}
if (x === y) {
return false;
}
}
return true;
}
function cal(n) {
var x = n;
var s = 0;
while (x > 0) {
console.log('s 1', s);
s = s + x % 10 * (x % 10);
x = Math.floor(x / 10);
console.log('s 2', s);
}
return s;
}
console.log('isHappy(19)', isHappy(19));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment