#include <stdbool.h>
static int next(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d * d;
n /= 10;
}
return sum;
}
bool isHappy(int n) {
int slow = n, fast = next(n);
while (fast != 1 && slow != fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast == 1;
}
class Solution {
int next(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d * d;
n /= 10;
}
return sum;
}
public:
bool isHappy(int n) {
int slow = n, fast = next(n);
while (fast != 1 && slow != fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast == 1;
}
};
package mathgeometry
func next(n int) int {
sum := 0
for n > 0 {
d := n % 10
sum += d * d
n /= 10
}
return sum
}
func isHappy(n int) bool {
slow, fast := n, next(n)
for fast != 1 && slow != fast {
slow = next(slow)
fast = next(next(fast))
}
return fast == 1
}
class Solution {
private int next(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d * d;
n /= 10;
}
return sum;
}
public boolean isHappy(int n) {
int slow = n, fast = next(n);
while (fast != 1 && slow != fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast == 1;
}
}
const next = (n) => {
let sum = 0;
while (n > 0) {
const d = n % 10;
sum += d * d;
n = Math.floor(n / 10);
}
return sum;
};
/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function (n) {
let slow = n, fast = next(n);
while (fast !== 1 && slow !== fast) {
slow = next(slow);
fast = next(next(fast));
}
return fast === 1;
};
class Solution:
def next(self, n: int) -> int:
total = 0
while n > 0:
n, d = divmod(n, 10)
total += d * d
return total
def isHappy(self, n: int) -> bool:
slow, fast = n, self.next(n)
while fast != 1 and slow != fast:
slow = self.next(slow)
fast = self.next(self.next(fast))
return fast == 1
impl Solution {
fn next(mut n: i32) -> i32 {
let mut sum = 0;
while n > 0 {
let d = n % 10;
sum += d * d;
n /= 10;
}
sum
}
pub fn is_happy(n: i32) -> bool {
let (mut slow, mut fast) = (n, Self::next(n));
while fast != 1 && slow != fast {
slow = Self::next(slow);
fast = Self::next(Self::next(fast));
}
fast == 1
}
}