dontWorry {FunWithNumbers}R Documentation

Test for Happy Numbers ~~

Description

This function calculates the sequence sum((digits_of_x)^p) to determine whether the value x is a "happy" number in the specified base.

Usage

dontWorry(x, pwr = 2, base = 10, maxiter = 100)

Arguments

x

The integer, bigz or character string representing an integer .

pwr

The exponent defining the power to which each digit is raised.

base

The base of the input x . Bases 2 through 36 are supported.

maxiter

A "safety switch" to avoid possible lengthy runtimes (when starting with very very large numbers), terminating the function prior to convergence.

Details

The dontWorry sequence follows simple rules: Take each digit in the current base and raise it to the specified power. Take the sum as the next input. If the sequence converges to 1, the number is happy. If the sequence ends in a cycle, the number is not happy.

Value

A list containing: isHappy: a logical value indicating whether or not x is happy given the specified base and power. theseq: the sequence of values found cyclic: TRUE if a cycle is found. (it should always be the case that cyclic = !isHappy)

Author(s)

Carl Witthoft, carl@witthoft.com

References

https://en.wikipedia.org/wiki/Happy_number

Examples

(dontWorry(20))
# $isHappy
#[1] FALSE
#$theseq
#[1] "20"  "4"   "16"  "37"  "58"  "89"  "145" "42"  "20" 
#$cyclic
#[1] TRUE 
(dontWorry('2254', base = 6))
# $isHappy
# [1] TRUE
# 
# $theseq
# [1] "2254" "121"  "10"   "1"   
# 
# $cyclic
# [1] FALSE

[Package FunWithNumbers version 1.2 Index]