26
\$\begingroup\$

Originally the Multiplicative digital root

Challenge

Basically do what the title says

Method

Given a positive integer 1 <= N <= 100000000 through one of our standard input methods, multiply every digit together, ignoring zeroes.

Ex: Take a number, say 361218402:

  • 3 * 6 = 18
  • 18 * 1 = 18
  • 18 * 2 = 36
  • 36 * 1 = 36
  • 36 * 8 = 288
  • 288 * 4 = 1152
  • 1152 * 1 (ignore zeroes or turn them into ones) = 1152
  • 1152 * 2 = 2304

The output for 361218402 is 2304

Test Cases

1 => 1
every other digit > 0 => itself
10 => 1
20 => 2
100 => 1
999 => 729
21333 => 54
17801 => 56
4969279 => 244944
100000000 => 1

Standard Loopholes are disallowed, and this is , so shortest byte count wins!

Congrats to Jo King who got the bounty with his 70 byte brain-flak answer!

\$\endgroup\$
6
  • 5
    \$\begingroup\$ I'd rather call this non-zero digital product. "root" suggests it reduces to a single digit, which isn't always true here. \$\endgroup\$ – Erik the Outgolfer Jan 15 '18 at 20:46
  • 1
    \$\begingroup\$ Can we take input as a string? Or (pushing it) an array of digits? \$\endgroup\$ – Shaggy Jan 15 '18 at 21:19
  • \$\begingroup\$ @EriktheOutgolfer Yes, however, if you repeat the process enough times, it does appear to always go to a single digit. \$\endgroup\$ – DJMcMayhem Jan 15 '18 at 21:21
  • \$\begingroup\$ You can take quoted input, but no, you can't take a pre-parsed list of digits if that's what you're asking \$\endgroup\$ – RaviRavioli Jan 15 '18 at 21:21
  • 7
    \$\begingroup\$ If we have to support to a max of 100000000000 I suggest the test case 99999999999 => 31381059609, since it doesn't fit in a default 32-bit integer. Perhaps better would be to lower the maximum output to a 32-bit maximum (2147483647). \$\endgroup\$ – Kevin Cruijssen Jan 16 '18 at 8:27

63 Answers 63

1 2
3
1
\$\begingroup\$

Java 8, 52 bytes

n->(n+"").chars().reduce(1,(x,y)->x*=(y-=48)>0?y:1);

Try it online!
Obligatory stream answer.

\$\endgroup\$
0
\$\begingroup\$

PHP, 52 Bytes

$t=1;foreach(array_filter(str_split($n))as$b)$t*=$b;
\$\endgroup\$
0
\$\begingroup\$

tinylisp, 67 bytes

(load library
(d f(q((N)(i N(*(i(mod N 10)(mod N 10)1)(f(/ N 10)))1

Try it online!

Explanation + ungolfed

The function f computes the non-zero digit product of its argument N. It uses a divmod algorithm to process the digits: If N is nonzero, it computes (N mod 10 if N mod 10 is nonzero, 1 otherwise), and multiplies that quantity by the result of recursing on N divided by 10. Once N becomes zero, all the digits have been processed and we return 1.

(load library)

(def digit-product
 (lambda (num)
  (if num
   (*
    (if (mod num 10) (mod num 10) 1)
    (digit-product (/ num 10)))
   1)))

This implementation commits the cardinal sin of not using tail recursion. It's not a problem for the size of number this challenge requires us to handle, but for big enough numbers, we would eventually hit the max recursion depth. Here's a proper tail-recursive implementation (with helper function) in 83 bytes:

(load library
(d _(q((N P)(i N(_(/ N 10)(*(i(mod N 10)(mod N 10)1)P))P
(q((N)(_ N 1

Try it online!

Ungolfed:

(load library)

(def _digit-product
 (lambda (num accum)
  (if num
   (_digit-product
    (/ num 10)
    (*
     (if (mod num 10) (mod num 10) 1)
     accum))
   accum)))

(lambda (num) (_digit-product num 1))
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.