IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 103 forks source link

Happy numbers made easy? #7889

Open rdstern opened 2 years ago

rdstern commented 2 years ago

@lloyddewit perhaps an example for the new script window!?

Happy numbers are fun! They are defined using sums of squares, but in a rather curious way. They are the sums of squares of the digits in each number. So ssqd(13) = 1^2 + 3^2 = 10. Then ssqd(10) = 1^2 + 0^2 = 1. That sequence means 13 is a happy number. If you do the same with 7, you find 7 is also a happy number. (The sequence is 7 > 49 > 97 > 130 - then that's like 13, so it gets to 1 also and is happy).

There is an article called Sums of Squares of Digits by Alan F Beardon. Here it is: The Mathematical Gazette Vol. 82, No. 495 (Nov., 1998), pp. 379-388 (10 pages) Published By: The Mathematical Association.

More generally here he is on happy numbers:

image

I have proposed adding a key in our calculator for Happy numbers. (There is a function in the Zseq package) But understanding them needs the ssq of the digits. That's (of course) another sequence in the Encyclopedia of Integer Sequences. Here: image

There is also code for producing these ssqd sequences in this reference - but not in R. Here, from the encyclopedia, is the code (apparently) in Python:

(Python)
def [A003132](https://oeis.org/A003132)(n): return sum(int(d)**2 for d in str(n)) # [Chai Wah Wu](https://oeis.org/wiki/User:Chai_Wah_Wu), Apr 02 2021

So I asked Sam Dumble from Stats4SD for a function in R and here is his response:

Hi Roger,
Always down for a fun R puzzle to distract me from real work, and need to get my R brain engaged ahead of the course starting up again next month!
Not quite one line, but something like this would work for summing up each digit (trying to be better disciplined at using map rather than going for for()/while() loops!):

library(tidyverse)
n<-765
1:nchar(n) %>%
  map_dbl(~as.numeric(substr(n,.x,.x))**2) %>%
  sum()

Would only work for a single integer – probably the easiest way to allow for a vector of inputs would be to turn into a function and then loop.

sum_sq_digits<-function(n){
   1:nchar(n) %>%
    map_dbl(~as.numeric(substr(n,.x,.x))**2) %>%
    sum() 
}
map_dbl(c(11,12,13,14),~sum_sq_digits(.x))

I guess you could allow for a vector input and do the looping over multiple integers within the function as well if you wanted but would be a little trickier or there is probably a more elegant way here that I am not thinking of right now that could do it in fewer lines (probably with imap() or reduce()).

Or the attached code could turn it into a full “happy” function.

He added a function that goes further and (I think) checks whether a number is happy. happy_digits.zip

I wonder (instead) about code - maybe including a function - the get the sum-of-squares of the digits. But then perhaps to go further and produce further columns that continue the sequence. So it would go, perhaps showing: x1 x2 x3 x4 x5 x5 x6 x7 1 1 1 1 1 1 1 1 1 1 2 4 16 37 58 89 145 42 20 4 3 9 81 65 61 37 58 89 145 42 20 4 4 16 etc (The first variable is just the numbers from 1 upwards. The second is the sum of squares of the digits. Then the third should be a simple function from the first and second variables.
The 4th is the same Perhaps we go to about 12 variables? The result is the data frame, which can then be saved or used.

This could be saved as a data-frame and then used in the library. Or the program could be run.

Perhaps this could become an example using the new improved script windows in R-Instat?

We should also (maybe) follow Alan Beardon and write about them! 4 16 37 58 89 145 42 20 4

lloyddewit commented 1 year ago

Note: The code provided above works correctly in the script window in PR #8551.

rdstern commented 1 year ago

@lloyddewit that's great. I have started thinking of the teaching needed and note the importance given to functions. There is another issue I raised elsewhere about perhaps adding a functions keyboard and happy numbers could be there.

That cannot be for this year. But I would still like some interesting functions to go into our script library. I'd like to link to this issue soon, and then we can close it.

I hope to have time, before the main release to tidy the issues.