ZCubes/Happy Numbers

From ZCubes Wiki
Jump to navigation Jump to search

Happy Numbers


This video demonstrates how to generate Happy Numbers. Happy Numbers are numbers of which the sum of squares of digits is computed and then this process is repeated with the computed numbers also until we obtain the result as 1. In some cases 1 is not obtained, so those are termed sad numbers.

Using ZCubes, you can provide a large range of numbers to compute which are the happy numbers in that set. Enjoy watching. Click Subscribe to know more of such interesting numbers.

ZCubes is an omnifunctional platform using which you can compute, do analytics, create graphs, draw, paint, create presentations, web pages and do much more.

Video


Happy Numbers















ZCubes Code


function dohappy(n)
{
	prev={}
	prevnums=[]
	originaln=n
	do
	{
	  	if(prev[n])
		{
		  console.log("MELCOIL",originaln,prevnums.concat(n))
		  return("MELCOIL")
		}
		prev[n]=true;
		prevnums.push(n)
		d=DIGITS(n)
		n=SUM(d.$(x=>POWER(x,2)))
	}while(prev[n]||n>1)
	console.log("HAPPY",originaln,prevnums.concat(n))
	return("HAPPY")
}
dohappy#;
dohappy(2..100)

Output:-

7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193 are Happy numbers in the range of 2 to 100.




© Copyright 1996-2021, ZCubes, Inc.