Other Clojure solutions.
(ns armstrong-numbers)
(defn digits [num]
(map #(Character/digit % 10) (str num)))
(defn expt [x n]
(reduce * (repeat n x)))
(defn armstrong? [n]
(= n
(reduce +
(map
(fn [x] (expt x (count (str n))))
(digits n)))))
Other Elm solutions.
module ArmstrongNumbers exposing (isArmstrongNumber)
isArmstrongNumber : Int -> Bool
isArmstrongNumber num =
let
digits =
String.fromInt num
in
digits
|> String.split ""
|> List.map
(String.toInt
>> Maybe.withDefault 0
>> (\i -> i ^ String.length digits)
)
|> List.foldl (+) 0
|> (==) num
Other Roc solutions.
module [isArmstrongNumber]
isArmstrongNumber : U64 -> Bool
isArmstrongNumber = \number ->
digits =
getDigits number
digitSum =
List.walk
digits
0
\sum, digit ->
pow =
Num.powInt digit (List.len digits)
sum + pow
digitSum == number
getDigits : U64 -> List U64
getDigits = \number ->
bytes =
Num.toStr number
|> Str.toUtf8
|> List.map (\byte -> byte - '0')
|> List.map Num.toU64
bytes
Other C solutions.
#include "armstrong_numbers.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
int char_to_int(char c)
{
return c - '0';
}
int is_armstrong_number(int candidate)
{
char s[16] = {'\0'};
sprintf(s, "%d", candidate);
int len = strlen(s);
int sum = 0;
for (int i = 0; i < len; i++)
sum += pow(char_to_int(s[i]), len);
return candidate == sum;
}