2

Armstrong Numbers are numbers where the number itself is equal to the sum of its digits raised to the power of the number of its digits.

Example:

  • 153 = 1^3 + 5^3 + 3^3
    ...so, 153 is Armstrong Number.

  • 142 != 1^3 + 4^3 + 2^3
    ...so, 142 is not an Armstrong Number.

Can somebody help me in writing codes for all 3 digits Armstrong number in R?

slamballais
  • 3,161
  • 3
  • 18
  • 29
user2776387
  • 67
  • 1
  • 3
  • 5
    `armnum<-c(153,370,371,407); print(armnum)` . To quote wikipedia, "These are odd facts, very suitable for puzzle columns and likely to amuse amateurs, but there is nothing in them which appeals to the mathematician" . – Carl Witthoft Sep 13 '13 at 12:42
  • 5
    Sure we can help, but you should show some effort. – Ferdinand.kraft Sep 13 '13 at 12:48
  • This [solution](http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html) in Fortran might help. – Jost Sep 13 '13 at 13:01
  • 2
    Downvoters, you should give the total newbie OP a chance to update their question! They haven't been seen since the initial post! – Simon O'Hanlon Sep 13 '13 at 13:10
  • 1
    Hi, rough welcome to SO. Since you are new here, you might want to read the [**about**](http://stackoverflow.com/about) and [**FAQ**](http://stackoverflow.com/faq) sections of the website to help you get the most out of it and to avoid getting massively downvoted! Please also read [**how to make a great reproducible example**](http://stackoverflow.com/q/5963269/1478381) and update your question accordingly. – Simon O'Hanlon Sep 13 '13 at 13:10
  • -1 You didn't seem to heed any advice and haven't updated your question. – Simon O'Hanlon Sep 13 '13 at 15:12

3 Answers3

13

A quick and dirty solution using ?strsplit:

armstrong <- function(x) {
  tmp <- strsplit(as.character(x), split="")  
  y <- sapply(tmp, function(y)sum(as.numeric(y)^length(y)))
  return(y == x)
}

E.g.:

armstrong(c(153, 142))
# [1] TRUE FALSE

# find all 3 digit numbers:
s <- 100:999
s[armstrong(s)]
# [1] 153 370 371 407
# @CarlWitthoft: wikipedia was right ;)
slamballais
  • 3,161
  • 3
  • 18
  • 29
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • 1
    `100:999` instead of `seq`? – dayne Sep 13 '13 at 12:57
  • 3
    While Armstrong numbers are of little use, this code snippet is a nice example of how to play with the digits of integers. But I should give you -1 for mipselling "Armstrong" :-) – Carl Witthoft Sep 13 '13 at 14:52
6

A variation on a theme...

I have in my R snippets a function from Greg Snow. I'll see if I can dig up a link later. Here's the original answer to a somewhat similar question. It's called "digits" and serves to split up a number into digits without using strsplit.

digits <- function(x) {
  if(length(x) > 1 ) {
    lapply(x, digits)
  } else {
    n <- ceiling(log10(x))
    x %/% 10^seq(0, length.out=n) %% 10
  }
}

Using that function, we can do something like:

A <- 100:999
A[sapply(digits(A), function(y) sum(y^length(y))) == A]
# [1] 153 370 371 407

It is, unfortunately, the slowest of the three functions here :(

slamballais
  • 3,161
  • 3
  • 18
  • 29
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • +1 for this great example (nearly) without casting to `character`. You could replace `nchar` by `n <- floor(log10(x))+1L` to get rid of the last `as.character` conversion (will be a little bit faster on my machine). – sgibb Sep 13 '13 at 14:24
  • 2
    And for this application you could drop the call to `rev` as well, since you are just cubing and adding the digits, order does not matter. – Greg Snow Sep 13 '13 at 14:53
1
(a <- rowSums(matrix(as.numeric(unlist(strsplit(as.character(100:999),''))),nrow=900,byrow=TRUE)^3))[a==100:999]
[1] 153 370 371 407
Tomas Greif
  • 21,685
  • 23
  • 106
  • 155
  • @CarlWitthoft, was that comment directed towards my answer perhaps? It is definitely the slowest of the three, and I think this one is the fastest. Thomas, there's no need to smash everything on one line. Perhaps that's the memory hog that Carl is referring to! :) – A5C1D2H2I1M1N2O1R2T1 Sep 13 '13 at 15:43
  • @AnandaMahto no, I was guessing that Tomas' creation of a gigundo matrix was the biggest RAM-eater. Your approach just creates a 1-dim list, right? – Carl Witthoft Sep 13 '13 at 16:41
  • @CarlWitthoft OK, I'll use more lines next time. It was not my intention to create best solution (my R knowledge is limited), but rather than that to come up with different approach (good mental exercise on Friday afternoon) :) – Tomas Greif Sep 13 '13 at 17:47