47
7
My friend and I were working on a lab in our AP Computer Science class and decided to code golf one one the problems since we still had half the class free after we finished. Here is the question:
Given a number n, is n divisible by each of its digits?
For example, 128 will pass this test- it is divisible by 1,2, and 8. Any numbers with a zero automatically disqualify the number. While you may use other languages and post solutions with them if you like, we are most interested in seeing how compact people can make the program in Java, as that is the language we use in the class. So far, we both have 51. Here is my current code:
public boolean dividesSelf(int n){for(int p=n;n%10>0;)n/=p%(n%10)>0?.1:10;return n<1;}
// 51 characters
// Breakdown:
// for(int p=n; Saves one semicolon to put declaration into for loop
// n%10>0;) Basic check-for-zero
// n/= Pretty simple, discarding one number off of n at a time
// p%(n%10)>0? If p (the given value) is not divisible by n%10 (the current digit)...
// .1:10; Divide by .1 (multiply by 10) so it fails the check next iteration. If it is divisible, divide by 10 to truncate the last digit
// return n<1 If the number was fully divisible, every digit would be truncated, and n would be 0. Else, there would still be non-zero digits.
Requirements
The method signature can be whatever you want. Just count the function body. Make sure, though, that the method returns a boolean value and only passes in one numeric parameter (not a string).
The code must be able to pass all of these cases (in order to stay true to the directions of the original question, only boolean true and false values count if the language supports booleans. If and only if your language does not have boolean variables you may represent false with 0 and true with any nonzero integer (preferably 1 or -1):
128 -> true
12 -> true
120 -> false
122 -> true
13 -> false
32 -> false
22 -> true
42 -> false
212 -> true
213 -> false
162 -> true
204 -> false
Also, we didn't count whitespace, so feel free to do the same, unless the whitespace is essential to the working of the program (so newlines in Java don't count, but a single space between int
and x=1
does count.)
Good luck!
18
Welcome to PPCG! A few suggestions: 1. Not counting functional whitespace is a bad idea. Any answer written in Whitespace will automatically win. 2. Should our submission print/return
– Dennis – 2014-11-26T03:37:24.680true
andfalse
or are truthy/falsy values OK as well? 3. Thejava
tag doesn't really apply here, as the challenge itself is unrelated to Java.Okay. sorry for the issues. Just to clear it up, would you consider the space in 'int p=n' to be functional, because I did not previously. I will fix the other issues you pointed out. – Mathew Kirschbaum – 2014-11-26T03:41:44.830
5All whitespace required for the code to work is functional. – FryAmTheEggman – 2014-11-26T03:42:15.673
Okay, thanks for the response! – Mathew Kirschbaum – 2014-11-26T03:47:00.307
Just count the calculation inside the brackets
- does this apply to all languages that can define functions? – August – 2014-11-26T04:26:34.693Yes, only count the code that is actually doing work, so ignore anything that defines the function but does not contribute to the actual implementation. Hopefully that clears it up. – Mathew Kirschbaum – 2014-11-26T04:42:03.673
Note that 0 is a multiple of 0, which makes me wonder how the answers would change if you didn't specifically exclude that. – None – 2014-11-26T16:35:42.780
One parameter, ok, but string type or numeric? – edc65 – 2014-11-26T16:37:16.240
Numeric inputs only. – Mathew Kirschbaum – 2014-11-26T17:46:10.670
1@RickyDemer: since 0 would be an exceptional input in that case (it's the only number with
0
digits that is a multiple of each of them), I imagine most answers would just get longer in an uninteresting way to include a check for it. So I like the problem as posed by the title better (divisible by its digits, rather than being a multiple of its digits, which excludes 0). – Jeroen Mostert – 2014-11-26T22:07:09.320I'm tempted to suggest the alternate interpretation of the question, in which dividing 123412341234 by 3 yields [12, 412, 412, 4]. – keshlam – 2014-11-28T04:39:11.670