| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | AWK | 250331T155456Z | xrs |
| nan | 230616T031026Z | Dadsdy | |
| 029 | Arturo | 230615T004132Z | chunes |
| 061 | Rockstar | 230614T224114Z | Shaggy |
| 003 | Thunno 2 | 230614T172345Z | The Thon |
| 021 | Factor + math.extras math.unicode | 221012T080851Z | chunes |
| 003 | Vyxal | 221012T071938Z | DialFros |
| 046 | C# Visual C# Interactive Compiler | 190923T164755Z | auhmaan |
| 052 | Java 8 | 190310T042218Z | Benjamin |
| 097 | C# | 180301T165626Z | James m |
| 019 | Attache | 180116T004828Z | Conor O& |
| 025 | Ruby + n | 180120T124328Z | Asone Tu |
| 032 | JavaScript | 180115T225335Z | Shaggy |
| 4437 | Stax | 180222T054142Z | Weijun Z |
| 047 | Microsoft Excel 47 Bytes | 180116T080711Z | Jarom C |
| 014 | Befunge98 PyFunge | 180216T194943Z | MercyBea |
| 021 | Octave | 180115T210352Z | Steadybo |
| 040 | R | 180115T224406Z | Giuseppe |
| 067 | tinylisp | 180120T070145Z | DLosc |
| 027 | Haskell | 180115T221159Z | totallyh |
| 004 | Jelly | 180119T004749Z | chromati |
| 013 | J | 180115T204121Z | cole |
| 070 | BrainFlak | 180118T012359Z | Jo King |
| 052 | PHP | 180118T044458Z | th3pirat |
| 030 | JavaScript Node.js | 180117T212516Z | zworek |
| 007 | Pip | 180117T200547Z | DLosc |
| 006 | Pyth | 180117T192240Z | Steven H |
| 026 | Julia 0.6 | 180117T082658Z | Frames C |
| 034 | Pari/GP | 180117T141930Z | alephalp |
| 045 | Java OpenJDK 8 | 180117T134457Z | Olivier |
| 078 | C# .NET Core | 180117T110223Z | kakkarot |
| 027 | Ruby | 180115T214823Z | Håv |
| 007 | Pyth | 180117T000933Z | Dave |
| 039 | C gcc | 180115T204348Z | Steadybo |
| 033 | Julia 0.6 | 180116T233159Z | gggg |
| 083 | SNOBOL4 CSNOBOL4 | 180116T211247Z | Giuseppe |
| 007 | K oK | 180115T212353Z | mkst |
| 051 | Java 8 | 180116T082449Z | Kevin Cr |
| nan | Perl 5 | 180116T153758Z | Xcali |
| 033 | JavaScript Node.js | 180115T223255Z | Wilson J |
| 058 | Javascript | 180116T113439Z | david |
| 068 | Red | 180116T113623Z | Galen Iv |
| 040 | dc | 180116T055614Z | Wildcard |
| 004 | Husk | 180116T084634Z | ბიმო |
| 005 | Brachylog | 180116T072224Z | Fatalize |
| 022 | Befunge | 180115T233434Z | James Ho |
| 078 | Batch | 180116T004337Z | Neil |
| nan | 180116T001637Z | Brad Gil | |
| 023 | Bash + coreutils + sed + bc | 180115T213403Z | Dennis |
| 005 | MATL | 180115T230304Z | Luis Men |
| 005 | Japt | 180115T211856Z | Shaggy |
| 028 | JavaScript ES6 | 180115T223135Z | Arnauld |
| 009 | Pyth | 180115T215836Z | KarlKast |
| 045 | Lua | 180115T214312Z | Jonathan |
| 007 | APL Dyalog | 180115T203951Z | Uriel |
| 062 | Forth gforth | 180115T212631Z | reffu |
| 056 | Clojure | 180115T210533Z | Carcigen |
| 088 | BrainFlak | 180115T203714Z | DJMcMayh |
| 004 | Jelly | 180115T204027Z | caird co |
| 044 | Python 2 | 180115T204227Z | Chas Bro |
| 003 | Pyt | 180115T211302Z | mudkip20 |
| 009 | APL+WIN | 180115T210848Z | Graham |
| 055 | Clean | 180115T210652Z | Οurous |
| 012 | Add++ | 180115T205548Z | caird co |
| 034 | Python 2 | 180115T205002Z | Dennis |
| 043 | Python 2 | 180115T204942Z | Rod |
| 019 | ><> | 180115T204605Z | Emigna |
| 005 | Jelly | 180115T202106Z | DJMcMayh |
| 004 | 05AB1E | 180115T203526Z | Emigna |
| 036 | Haskell | 180115T203017Z | Cristian |
AWK, 32 bytes
{for(x=1;++i<NF;)x*=$i?$i:1}$0=x
{for(x=1; # no multiply by zero
++i<NF;) # until last digit
x*= # product
$i?$i:1 # check if digit is zero
}$0=x # set output
(,), 175 Chars or \$175\log_{256}(3)\approx34.67\$ Bytes
(()()()(),()()()()()()())(()(),())(,((),(())(),,,(()()())(),(())(()()()(),,,,(),,(()()()())))(()(),(()(),,,,(),,(())))((),(,,,,,())),,,(,(()()(),((()))))(()()()),())(,,(()()))
Arturo, 29 bytes
$=>[∏filter digits&=>[0=&]]
$=>[ ; a function; assign input to &
∏ ; product of...
filter digits&=>[ ; digits of the input that aren't...
0=& ; equal to zero
] ; end filter
] ; end function
Rockstar, 61 bytes
Listen to N
Cut N
O's 1
While N
Let O be*roll N-0 or 1
Say O
Try it (Code will need to be pasted in)
Thunno 2, 3 bytes
0op
Explanation
0op # Implicit input
0o # Remove 0s
p # Product
# (Yes, both of those work on numbers)
# Implicit output
Factor + math.extras math.unicode, 21 bytes
[ 48 v-n nonzero Π ]
Takes input as a string and gives output as an integer.
48 v-nSubtract 48 from each code point in the input, converting it to a list of digits.nonzeroTake only the non-zero numbers.ΠCalculate the product of a sequence.
Vyxal, 3 bytes
fꜝΠ
Pyt isn't the only language to do 3 bytes :3
Explained
fꜝΠ
f # flatten to a list
ꜝ # remove all 0's
Π # product
C# (Visual C# Interactive Compiler), 46 bytes
i.Select(c=>c>48?c-48:1).Aggregate((p,c)=>p*c)
Java 8, 52 bytes
n->(n+"").chars().reduce(1,(x,y)->x*=(y-=48)>0?y:1);
Try it online!
Obligatory stream answer.
C#, 97 Bytes (First code golf)
static int X(int y){var z=y.ToString();int r=1;foreach(var q in z){if(q!=48){r*=q-48;}}return r;}
Not sure if i had to wrap it in a method or not so just included it to be safe.
Takes an Int in, converts it to a string and returns the multiple of each of the characters ignoring 0's. Had to minus 48 due to the program using the ascii value as it reads it as a char.
Attache, 20 19 bytes
Prod##Max&1=>Digits
Try it online! Function that takes an integer as an argument and returns an integer.
Explanation
This first takes the Digits of the input number, then each number's Max with 1 is taken, then the Product is taken.
Other solutions
Prod@Larger&1@Digits
Prod@Map[Max&1]@Digits
Prod@Map&:(Max&1)@Digits
{Prod[Max&1=>Digits[_]]}
JavaScript, 33 32 bytes
Feels like ages since I've done an array mapping solution in JS!
Takes input as a string.
n=>[...n].map(x=>t*=+x||1,t=1)|t
Try it
o.innerText=(f=
n=>[...n].map(x=>t*=+x||1,t=1)|t
)(i.value="361218402");oninput=_=>o.innerText=f(i.value)
<input id=i type=number><pre id=o>
Stax, 4 bytesCP437
┤caü
5 bytes when unpacked,
E0-k*
Explanation
E Convert number to digits
0- Remove all zeros
k* Reduce array with multiplication
Microsoft Excel - 62 47 Bytes
{=PRODUCT(IFERROR(1/(1/MID(A1,ROW(A:A),1)),1))}
Explanation
Converts value in cell A1 to array of digits, changes 0s to 1s using absolute value, then multiplies.
15 bytes saved thanks to Engineer Toast
Octave, 21 bytes
Thanks to @Luis Mendo for saving a byte and thanks to @alephalpha for saving another byte!
@(n)prod((k=n-48)+~k)
Takes input as a string of digits.
30 bytes:
@(n)prod((k=num2str(n)-48)+~k)
Takes input as a number.
R, 40 bytes
cat(prod((x=scan()%/%10^(0:12)%%10)+!x))
Since input is guaranteed to have no more than 12 digits, this should work nicely. Computes the digits as x (including leading zeros), then replaces zeros with 1 and computes the product.
cat( #output
prod( #take the product of
(x= #set X to
scan() #stdin
%/%10^(0:12)%%10) #integer divide by powers of 10, mod 10, yields digits of the input, with leading zeros. So x is the digits of the input
+!x #add logical negation, elementwise. !x maps 0->1 and nonzero->0. adding this to x yields 0->1, leaving others unchanged
))
tinylisp, 67 bytes
(load library
(d f(q((N)(i N(*(i(mod N 10)(mod N 10)1)(f(/ N 10)))1
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
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))
Haskell, 27 bytes
foldr((*).max 1.read.pure)1
Ungolfed with UniHaskell and -XUnicodeSyntax
import UniHaskell
f ∷ String → Int
f = product ∘ map (max 1 ∘ read ∘ pure)
Explanation
I'll start with what I initially had:
product.map(max 1.read.pure)
This is a point-free expression that evaluates to a function taking a string (or a list of characters) s ("301") as an argument. It maps max 1.read.pure over s, essentially taking each character i, injecting it into a list (which makes it a string) (["3", "0", "1"]), then reading it, which evaluates the string ([3, 0, 1]) and finally taking the greater of i and 1 ([3, 1, 1]). Then it takes the product of the resulting list of integers (3).
I then golfed it by a byte with:
foldr((*).max 1.read.pure)1
This works because product is equivalent to foldr (*) 1. Instead of mapping and folding, I combined the two by folding with (*).max 1.read.pure which takes each non-zero digit and multiplies it with the accumulator.
J, 17 14 13 bytes
-4 bytes courtesy of @GalenIvanov
[:*/1>.,.&.":
Probably can be improved some. Edit: and so it was.
Explanation
[: */ 1 >. ,.&.":
": Convert to string
,. Convert row to column vector
&. Convert to numbers
1 >. Maximum of each element with 1 (convert 0 to 1)
*/ Product
[: Cap fork
&.-under is a nifty adverb that applies the verb on the right, then the verb on the left, then the inverse of the verb on the right. Also converting back to numbers is technically using eval (".-do).
Brain-Flak, 74 72 70 bytes
-2 thanks to Nitrodon for suggesting getting the negation of the number so that you only have to increment rather than decrement later
{([{}]((((()()()){}){}){}){}){({<({}())><>([[]](){})<>}<><{}>)<>}{}}<>
There might be a few ways to golf this further, such as redoing the multiplication to avoid having to initialise the total with 1. (-2 bytes)
How It Works:
{ Loop over every number
([{}]((((()()()){}){}){}){}) Add 48 to the negative of the ASCII to get the negation of the digit
{ If the number is not 0
({<({}())><>([[]](){})<>}<><{}>)<> Multiply the total by the number
If the total is on an empty stack, add 1
}
{} Pop the excess 0
}<> Switch to the stack with the total
PHP, 52 Bytes
$t=1;foreach(array_filter(str_split($n))as$b)$t*=$b;
JavaScript (Node.js), 30 bytes
f=([a,...b])=>a?(+a||1)*f(b):1
Takes string as an input, treats it as array, and by array destructuring separates the first element [a,...b]. +a||1 returns digit corresponding to a character. I guess that rest is self explaining..
Pip, 7 bytes
$*YaDC0
Delete Character 0 from the argument a and fold ($) on multiplication (*). The Yank operator is used to adjust the precedence of the subexpression (one byte shorter than $*(aDC0)).
Pyth, 6 bytes
*F #sM
Explanation:
sM Get digits of implicit string input
# Filter on identity, removing 0s
*F Fold on multiplication
C# (.NET Core), 78 bytes
using System.Linq
n=>n.Replace("0","").Select(c=>c-'0').Aggregate(1,(a,b)=>a*b)
Ruby, 42 40 35 32 27 bytes
p eval (gets.chars-[?0])*?*
Assumes no newlines in input Major influence
-2 bytes thanks to @GolfWolf
-5 bytes thanks to @Conor O'Brien
Pyth, 7 bytes
*FsMs#Q
*FsMs#Q Full program, takes input in "" from stdin and prints to stdout
s#Q Filter input for truthiness of int-parse(digit), then
sM map int-parse to this,
*F then fold over multiplication
C (gcc), 39 bytes
k;f(n){for(k=1;n;n/=10)k*=n%10?:1;k=k;}
Needs to be compiled without optimizations (which is the default setting for gcc, anyway).
SNOBOL4 (CSNOBOL4), 83 bytes
DEFINE('O(N)')
O O =1
R N LEN(1) . X REM . N :F(RETURN)
X =EQ(X) 1
O =O * X :(R)
A function that takes input as a string.
K (oK), 10 7 bytes
Solution:
*/1|.:'
Example:
*/1|.:'"4969279"
244944
Explanation:
Convert the input string to a list of integers, take max compared to 1 (inspiration from the J solution) and then multiply over the list.
Evaluation is performed right to left.
*/1|.:' / the solution
.:' / value (.:) each (')
1| / or with 1 ( 0=>1, 1=>1, 2=>2 )
*/ / multiply (*) over (/)
Java 8, 55 54 53 51 bytes
int f(int n){return n>0?(n%10>0?n%10:1)*f(n/10):1;}
Port of @Dennis' Python 2 answer.
-1 byte thanks to @RiaD.
55 54 bytes version:
n->{int r=1;for(;n>0;n/=10)r*=n%10>0?n%10:1;return r;}
JavaScript (Node.js), 36 33 bytes
Simple Javascript (ES6) method that takes input as number string, spreads it into an array, then reduces it through multiplication or returns the value if the result is 0.
3 bytes saved thanks to Shaggy
s=>[...s].reduce((a,b)=>b*a||a,1)
Javascript, 58 bytes
f=a=>{b=1;for(i=0;a[i];i++){(a[i]>0)&&(b=+b*a[i])}alert(b)
Input via function(arg) outputs alert
Red, 68 bytes
func[n][p: 1 while[n > 0][d: n // 10 n: n / 10 if d > 0[p: p * d]]p]
Since 100000000000 is beyond the integer range of the Red language, here's another approach that solves this issue, using string manipulation:
Red, 74 bytes
func[n][p: 1 foreach c to-string n[if c >#"0"[p: p * to-integer c - 48]]p]
dc, 40 bytes
Sad to see dc so underrepresented here.
This is essentially my first code golf post, so if I've misjudged how bytes should be counted, go easy on me! :)
?[1+]sc[10~rdZ1<a]dsax1[rd0=c*z1<b]dsbxp
You can run this in the terminal like so:
$ echo 361218402 | dc -e '?[1+]sc[10~rdZ1<a]dsax1[rd0=c*z1<b]dsbxp'
2304
Or you can try it online!
Explanation (probably too verbose):
? # Read and execute a line from standard input
# (A number just goes on the stack)
[ # Begin a literal string (ended by a matched ']')
1 # Push 1 onto the stack (when this string is executed as a macro)
+ # Pop top two numbers, add them, push result
] # End literal string
s # Pop top of stack (the string) and store it in...
c # ...register c
[
10 # Push 10 (duh)
~ # Pop top two numbers, divide second popped by first popped, push quotient and then remainder
r # Reverse top two numbers of stack
d # Duplicate top of stack (pop and then push it twice)
Z # Pop top of stack and push its length in digits
1
< # Pop two numbers and compare; if first popped is less than second then...
a # ...run macro stored in register a (a recursive call, here)
] # End string
d # Duplicate top of stack
s # Store in...
a # ...register a
x # Execute the top of stack!
# At this point the stack will consist of one digit entries, the digits of input
1
[
r # Reverse
d # Duplicate
0
= # If top two numbers are equal then...
c # ...run macro in register c
* # Pop two, multiply, push product
z # Push "height" of stack
1
< # If height of stack is greater than 1 then...
b # ...run macro b
]
d # Duplicate the string on the stack
sb # Store it as macro b
x # Execute!
p # Print top of stack
Husk, 4 bytes
ΠfId
ΠfId -- implicit input N, for example 1607
d -- digigts: [1,6,0,7]
fI -- filter by identity function: [1,6,7]
Π -- product: 42
Brachylog, 5 bytes
⊇ẹ×ℕ₁
Explanation
⊇ Take a subset of the input
ẹ Split the subset into a list of digits
× Product
ℕ₁ This product must be in [1, +∞)
This works because ⊇ unifies from large subsets to small subsets, so the first one that will result in a non-zero product is when all zeroes are excluded and nothing else.
Befunge, 23 22 bytes
1<*_$#`.#0@#:+!:-"0"~$
Explanation
1< Push 1, turn back left, and push a second 1.
$ Drop one of them, leaving a single 1, the initial product.
-"0"~ Read a char and subtract ASCII '0', converting to a number.
+!: If it's 0, make it 1 (this is n + !n).
` 0 : Then test if it's greater than 0, to check for EOF.
_ If it is greater than 0, it wasn't EOF, so we continue left.
* Multiply with the current product, becoming the new product.
1< Now we repeat the loop, but this time push only a single 1...
$ ...which is immediately dropped, leaving the current product.
_ On EOF, the input value will be negative, so we branch right.
$ We don't need the input, so drop it.
. @ Leaving us with the product, which we output, then exit.
Batch, 78 bytes
@set/ap=%2+0,p+=!p,d=%1%%10,p*=d+!d,n=%1/10
@if %1 gtr 0 %0 %n% %p%
@echo %2
Previous 80-byte solution:
@set/an=%1,p=1
:l
@set/ad=n%%10,p*=d+!d,n/=10
@if %n% gtr 0 goto l
@echo %p%
(This version outputs 1 for an input of zero, rather than failing.)
Perl 6, 19 bytes
{[*] grep +*,.comb}
Expanded:
{ # bare block lambda with implicit parameter 「$_」
[*] # reduce using &infix:«*»
grep # find the values
+*, # that aren't "0"
.comb # split 「$_」 into digits (returns single character strings)
}
MATL, 5 bytes
!UXzp
Input is taken as a string
Try it at MATL Online! Or verify test cases in Try It Online!
Explanation
! % Implicit input: string (row vector of chars). Transpose into
% a column vector of chars
U % Convert from string to number. Treats each row independently,
% producing a column vector of numbers
Xz % Keep only nonzeros
p % Product. Implicit display
Japt, 5 bytes
ì f ×
Explanation
In order: split to an array of digits, filter to remove zeroes and reduce by multiplication.
JavaScript (ES6), 28 bytes
Designed for 32-bit integers.
f=n=>!n||(n%10||1)*f(n/10|0)
Test cases
f=n=>!n||(n%10||1)*f(n/10|0)
console.log(f(1)) // => 1
console.log(f(10)) // => 1
console.log(f(20)) // => 2
console.log(f(100)) // => 1
console.log(f(999)) // => 729
console.log(f(21333)) // => 54
console.log(f(17801)) // => 56
console.log(f(4969279)) // => 244944
Pyth, 10 9 Bytes
.U*s!BsZs
Explanation
.U*s!BsZs
.U Q Fold over the implicit input string
sZ Convert each char to int
s!B Form the list [d,not d] and sum it, so that 0 becomes 1
* sb Multiply by the prev product (implicit). s needed because 1st value is char
Forth (gforth), 62 bytes
: f 1 swap begin ?dup while 10 /mod -rot 1 max * swap repeat ;
Clojure, 56 bytes
(fn[n](apply *(replace{0 1}(map #(-(int %)48)(str n)))))
Pretty basic. Turns the number into a string, then subtracts 48 from each character to turn them back into numbers. It then replaces each 0 with a 1, and applies * to the resulting list of numbers (which reduces * over the list). Can accept a number, or a stringified number.
(defn non-zero-prod [n]
(let [; Abusing strings to get each digit individually
str-n (str n)
; Then turn them back into numbers
digits (map #(- (int %) 48) str-n)
; Substitute each 0 for a 1
replaced (replace {0 1} digits)]
; Then get the product
(apply * replaced)))
Brain-Flak, 88 bytes
Readable version:
#Push a 1 onto the alternate stack. Return to the main stack
(<>())<>
#While True:
{
#Push the current digit minus 48 (the ASCII value of '0') onto the alternate stack
({}[((((()()()){}){}){}){}]<>)
#If it's not 0...
{
(<
#Multiply the top two values (the current digit and the current product that started at 1)
({}<>)({<({}[()])><>({})<>}{}<><{}>)
#Also push a 0
>)
#Endwhile
}
#Pop the 0
{}
#Return to the main stack
<>
#Endwhile
}
#Toggle to the alternate stack, and implicitly display
<>
Jelly, 4 bytes
Do1P
Try it online! or see the test suite
How it works
Do1P - Main link. Argument: n (integer) e.g. 1230456
D - Digits [1, 2, 3, 0, 4, 5, 6]
o1 - Replace 0 with 1 [1, 2, 3, 1, 4, 5, 6]
P - Product 720
Pyt, 3 bytes
ąžΠ
Explanation:
ą Convert input to array of digits (implicit input as stack is empty)
ž Remove all zeroes from the array
Π Get the product of the elements of the array
APL+WIN, 9 bytes
×/(⍎¨⎕)~0
Prompts for screen input as a character string, convert to digits, drop zeros and multiply all.
Haskell, 36 bytes
f n=product[read[d]|d<-show n,d>'0']
Same byte count:
f n=product[max 1$read[d]|d<-show n]