33
\$\begingroup\$

You will be given a positive, whole number (that will never contain a 0) as input. Your task is to check whether it is a Lynch-Bell number or not.

A number is a Lynch-Bell number if all of its digits are unique and the number is divisible by each of its digits.

In fact, there are actually only 548 Lynch-Bell numbers, so hard-coding is a possibility, but will almost certainly be longer.

126 is a Lynch-Bell number because all of its digits are unique, and 126 is divisible by 1, 2, and 6.

You can output any truthy and falsy value.

Examples:

7 -> truthy
126 -> truthy
54 -> falsy
55 -> falsy
3915 -> truthy

This is OEIS A115569.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Related. (Asks for all the numbers instead of posing a decision problem.) \$\endgroup\$ Jul 4, 2017 at 11:54
  • 2
    \$\begingroup\$ Can I take input as a string? \$\endgroup\$ Jul 4, 2017 at 12:38
  • 3
    \$\begingroup\$ @TheLethalCoder Of course you can, that's a silly question. \$\endgroup\$
    – Okx
    Jul 4, 2017 at 12:40
  • 15
    \$\begingroup\$ @Okx Not all challenge posters are as flexible in their allowed inputs as you so always worth an ask. \$\endgroup\$ Jul 4, 2017 at 12:41

44 Answers 44

27
\$\begingroup\$

Mathematica, 42 bytes

0!=##&@@d&&##&@@((d=IntegerDigits@#)∣#)&

I think 0!=##&@@d&&##&@@ is a new low in readability for Mathematica...

Explanation

Some of the basic syntactic sugar used here:

  • & has very low precedence and turns everything left of it into an unnamed function.
  • && is just the And operator.
  • # is the argument of the nearest enclosing unnamed function.
  • ## is a sequence of all of the function's arguments.
  • @ is prefix notation for function calls, i.e. f@x == f[x].
  • @@ is Apply, which passes the elements of a list as individual arguments to a function, i.e. f@@{a,b,c} == f[a,b,c].

With that out of the way...

(d=IntegerDigits@#)

This should be fairly self-explanatory: this gives us a list of the input's decimal digits and stores the result in d.

(...∣#)

This tests the input for divisibility by each of its digits (because the divisibility operator is Listable). This gives us a list of Trues and Falses.

...&@@...

We apply the function on the left-hand side to the list of booleans, such that each boolean is a separate argument.

...&@@d

We apply another function to d, so that the individual digits are given as separate arguments. The function is 0!=##&, i.e. Unequal[0, d1, d2, ...]. It checks that all the digits are distinct (and that they are distinct from 0 but that's given by the challenge, and if it wasn't, it wouldn't be a divisor anyway). 0!=##& is really just a 1-byte saver on using Unequal itself, and it works because there's a 1-byte element (0) that we know is not present. So this first thing checks that the digits are unique. Let's call this result U

...&&##

Again, this is really just shorthand for And[U, ##]. With ## being a sequences, the individual booleans from the initial divisibility check are expanded into the And, so we get And[U, d1∣n, d2∣n, ...] which checks that both the digits are unique and each digit divides the input.

\$\endgroup\$
7
  • 6
    \$\begingroup\$ ##&@@d&&##&@@? What does that even do? \$\endgroup\$
    – Okx
    Jul 4, 2017 at 12:03
  • \$\begingroup\$ @Okx Added an explanation. \$\endgroup\$ Jul 4, 2017 at 12:12
  • \$\begingroup\$ May be you can replace 0!= by 0< ? \$\endgroup\$
    – sergiol
    Jul 4, 2017 at 14:26
  • \$\begingroup\$ @sergiol I'd have to sort the digits to do that. \$\endgroup\$ Jul 4, 2017 at 14:49
  • \$\begingroup\$ New low in readability indeed, normally Mathematica looks like a bunch of syntax-sugar around a few function names I can understand, I was unaware you could make a program entirely out of the sugar :p (of course, your excellent explanation let's me see that its of course not all sugar, but still, very impressive!) \$\endgroup\$
    – mbrig
    Jul 4, 2017 at 19:13
11
\$\begingroup\$

Python 3, 56 bytes

lambda n:any(int(n)%int(x)for x in n)or len(n)>len({*n})

Try it online!

Output False if it's IS a Lynch-Bell number, True otherwise.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Is this inputting as a string? \$\endgroup\$ Jul 5, 2017 at 17:18
  • 2
    \$\begingroup\$ This has two problems: 1) it doesn't give truthy/falsey answers as specified (it's just 4 bytes!); 2) it throws an exception on the input "10". Otherwise, very nice and concise! \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:33
  • \$\begingroup\$ @CalculatorFeline Yes. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:33
  • \$\begingroup\$ @CRDrost 1) the ouput well defined so there is no problem (usually) 2) there will never be 0 in the input \$\endgroup\$
    – Rod
    Jul 5, 2017 at 17:45
  • 1
    \$\begingroup\$ 1) I mean, there is a problem, which is that they asked for X and you did not give it. 2) Ah, you're totally right, I missed that completely. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:55
10
\$\begingroup\$

Jelly, 6 4 bytes

Dg⁼Q

Try it online!

How it works

Dg⁼Q  Main link. Argument: n

D     Decimal; convert n to base 10.
 g    Take the GCD of each decimal digit k and n.
      For each k, this yields k if and only if k divides n evenly.
   Q  Unique; yield n's decimal digits, deduplicated.
  ⁼   Test the results to both sides for equality.
\$\endgroup\$
1
  • 3
    \$\begingroup\$ There's also gQV= if you prefer an ASCII-only solution. \$\endgroup\$
    – Dennis
    Jul 4, 2017 at 16:00
9
\$\begingroup\$

Brachylog, 10 bytes

≠g;?z%ᵐ=h0

Try it online!

Explanation

≠             All digits are different
 g;?z         Zip the input with each of its digits
     %ᵐ       Map mod
       =      All results are equal
        h0    The first one is 0
\$\endgroup\$
6
\$\begingroup\$

C#, 87 83 bytes

using System.Linq;s=>s.Distinct().Count()==s.Length&s.All(c=>int.Parse(s)%(c-48)<1)

I wrote this in notepad before testing in Visual Studio, where it worked fine, so just realised I'm now that level of nerd...

Full/Formatted Version:

using System;
using System.Linq;

class P
{
    static void Main()
    {
        Func<string, bool> f = s => s.Distinct().Count() == s.Length
                                  & s.All(c => int.Parse(s) % (c - 48) < 1);

        Console.WriteLine(f("7"));
        Console.WriteLine(f("126"));
        Console.WriteLine(f("54"));
        Console.WriteLine(f("55"));
        Console.WriteLine(f("3915"));

        Console.ReadLine();
    }
}
\$\endgroup\$
1
6
\$\begingroup\$

JavaScript (ES6), 42 41 bytes

s=>![...s].some((e,i)=>s%e|s.search(e)<i)

Takes input as a string and returns true or false as appropriate. Edit: Saved 1 byte thanks to @RickHitchcock. Other versions:

Takes input as a string and returns 0 or 1 (i.e. logical inverse) for 40 bytes:

s=>/(.).*\1/.test(s)|[...s].some(e=>s%e)

Takes input as a number and returns 0 or 1 for 43 bytes:

n=>/(.).*\1/.test(n)|[...''+n].some(e=>n%e)

Takes input as a number and returns 1 or 0 for 45 bytes:

n=>!/(.).*\1/.test(n)&![...''+n].some(e=>n%e)
\$\endgroup\$
3
  • \$\begingroup\$ I was unfamiliar with the \n option for back-referencing. +1. You can move the test logic to the some method to save a byte: s=>![...s].some((e,i)=>s%e|s.search(e)<i) \$\endgroup\$ Jul 4, 2017 at 14:09
  • \$\begingroup\$ When I used [...new Array(9999999)].map((_,n)=>n+1+"").filter(s=>![...s].some((e,i)=>s%e|s.search(e)<i)).length I got 5081 instead of the expected 548, so this is not correct as written. Really tight code, though. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:25
  • \$\begingroup\$ Sorry, I retract my comment about this not being correct. My test code is not correct because the original poster expects that zeros have already been filtered out. With an extra .filter(x => x.indexOf('0')===-1) this returns 548 as promised. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 18:01
6
\$\begingroup\$

05AB1E, 4 bytes

ÑÃÙQ

Try it online!

Same algorithm as this answer to a related question.

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

Python 3, 54 bytes

Returns False when a number is a Lynch-Bell number. Takes strings as input. Came up with on my own but very similar to Rod's. I would've commented under his post but I don't have any reputation yet.

lambda s:len({*s})<len(s)+any(int(s)%int(c)for c in s)

Try it online!

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Stephen
    Jul 4, 2017 at 21:27
  • \$\begingroup\$ Welcome! Like Rod's, your function throws an exception on input "10". \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:34
  • 1
    \$\begingroup\$ @CRDrost "You will be given a positive, whole number (that will never contain a 0) as input." \$\endgroup\$
    – C McAvoy
    Jul 5, 2017 at 19:32
  • \$\begingroup\$ Right, I've posted comments everywhere else I complained about this, but I apparently missed this one. Sorry, retracted! \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 19:36
  • \$\begingroup\$ @CRDrost No worries! \$\endgroup\$
    – C McAvoy
    Jul 5, 2017 at 20:01
5
\$\begingroup\$

Regex (ECMAScript), 194 171 167 164 155 145 142 107 bytes

-35 bytes (142 → 107) thanks to H.PWiz; this is a complete reimplementation. The primary sources of bytes saved are using tail = floor((tail + Digit) / 10) - Digit instead of tail = floor(tail / 10) , and "factoring" the assertions – reusing the expression at the end to mean two different things.

^(?!(x{1,9})(((?=(x*)((\1\4){9}x*))\5)*(?=(x*)(\7{9}$))\8\1|(?!\1*$))((?=(x*)((\1\10){9}x*))\11)*(x{10})*$)

Try it online!

Takes its input in unary, as a sequence of x characters whose length represents the number.

This was an interesting challenge to solve without molecular lookahead or variable-length lookbehind. The restrictions result in a completely different algorithm being used.

Note that the above version does not reject numbers that have a zero as at least one of their digits, as per the challenge's rules. It costs 2 bytes 10 bytes to handle zero correctly, for a total of 144 117 bytes:

^(?!(x{0,9})(((?=(x*)((\1\4){9}x*))\5)*(?=(x*)(\7{9}$))\8\1|(?!\1*$))((?=(x*)((\1\10){9}x*))\11)*((x{10})+|(?!\1))$)x

Try it online!

The commented version below describes the 144 117 byte version:

^           # tail = N = input number
(?!         # Negative lookahead - Assert that there's no way for the following to match:
    (x{0,9})                       # \1 = conjectured digit; tail -= \1
    # Assert one of the following two alternatives:
    (
        # Assert that \1 occurs as a digit of N, and chop it away (and all the digits
        # right of it) from tail, returning only the digits left of its found instance.
        (
            (?=
                (x*)((\1\4){9}x*)  # \4 = floor((tail + \1) / 10) - \1;
                                   # \5 = tool to make tail = \4
            )
            \5                     # tail = \4
        )*
        (?=
            (x*)(\7{9}$)           # assert tail % 10 == 0; \7 = tail / 10;
                                   # \8 = tool to make tail = \7
        )
        \8\1                       # tail = \7 - \1 (all the digits of N left of where \1
                                   #                 was found, minus \1)
    |
        # Assert that \1 is not a divisor of N
        (?!\1*$)
    )
    # Assert that \1 occurs as a digit in tail+\1, which will either be a second occurrence
    # found further left in N of an already-found occurrence, or any occurrence of a digit
    # that is not a divisor of N.
    (
        (?=
            (x*)((\1\10){9}x*)     # \10 = floor((tail + \1) / 10) - \1;
                                   # \11 = tool to make tail = \10
        )
        \11                        # tail = \10
    )*
    # Assert one of the following two alternatives:
    (
        # Assert tail > 0 and tail % 10 == 0, which means we've found an occurrence of \1
        # that isn't the leftmost digit
        (x{10})+
    |
        # Assert tail == 0 and tail != \1, which means we've found that \1 is the leftmost
        # digit of N and \1 != 0 (which needs to be asserted, otherwise we'd always find
        # a phantom occurrence of the digit 0 left of the leftmost digit of N)
        (?!\1)
    )$
)
x                                  # Prevent N=0 from being matched

The version above is very slow, so I'm keeping my 144 byte solution here:

^(?!(?=(x*)\1{9})(x{0,9})(?=(x{10})*$|(.*(?=\1$)((?=(x*)(\6{9}x*))\7)*?\B(?=(x*)(\8{9}\2$))\9))((?!\2*$)|\4((?=(x*)(\12{9}x*))\13)*\2(x{10})*$))

Try it online!

^           # tail = N = input number
(?!         # Negative lookahead - Assert that there's no way for the following to match:
    (?=(x*)\1{9})                # \1 = floor(N / 10)
    (x{0,9})                     # \2 = take a guess at a numeral that may occur as any
                                 #      digit of N, even the rightmost one; tail -= \2
    # Assert that \2 occurs as a digit in N, and if it does, identify the rest of N to
    # the left of that digit, so that we can set tail equal to it later (so we can later
    # assert that \2 is not duplicated).
    (?=
        (x{10})*$                # Skip the rest if we picked the rightmost digit of N
    |
        (                        # \4 = tool to make tail = \8
            .*(?=\1$)            # tail = \1
            # Loop through chopping away each rightmost decimal digit of tail, one by one
            (
                (?=
                    (x*)         # \6 = floor(tail / 10)
                    (\6{9}x*)    # \7 = tool to make tail = \6
                )
                \7               # tail = \6
            )*?                  # Only iterate until we find the first match of the
                                 # following:
            \B                   # Leave at least one digit remaining (to avoid
                                 # thinking we've found a zero-digit when there isn't
                                 # actually one)
            (?=(x*)(\8{9}\2$))   # \8 = floor(tail/10); \9 = tool to make tail = \8;
                                 # assert that tail % 10 == \2, i.e. that our guessed
                                 # digit matches the current rightmost digit of tail
            \9                   # tail = \8
        )
    )
    # \2 is now one of the decimal digits of N.
    # Assert that either of the following two alternatives could match, and in the
    # context outside of the negative lookahead, that neither of them can match:
    (
        # Assert tail is not divisible by \2. It doesn't matter that tail == N-\2
        # instead of N, because that doesn't alter tail's divisibility by \2.
        (?!\2*$)
    |                            # or...
        # Assert that \2 doesn't occur as any decimal digit left of where it was found
        \4                       # tail = \8 if \8 is set, otherwise no change; this
                                 #        relies on ECMAScript NPCG behavior.
        # Chop away any number (minimum 0) of rightmost decimal digits of tail
        (
            (?=(x*)(\12{9}x*))   # \12 = floor(tail / 10);
                                 # \13 = tool to make tail = \12
            \13                  # tail = \12
        )*                       # Iterate any number of times, minimum 0
        \2                       # tail -= \2
        (x{10})*$                # assert tail is divisible by 10
    )
)

Regex (ECMAScript + (?*)), 81 bytes

^(?!(?*(((?=(x*)(\3{9}(x*)))\4)+))((?!\5+$)|\1((?=(x*)(\8{9}x*))\9)*\5(x{10})*$))

This challenge is much easier to solve with molecular lookahead; it's the version I implemented first.

Working on this alerted me to the presence of a bug in my regex engine (now fixed).

^           # tail = N = input number
(?!         # Negative lookahead - Assert that there's no way for the following to match:
    (?*     # Molecular lookahead - cycle through all possible matches of the following:
        (                            # \1 = tool to make tail = the final value of \3
            # Loop through chopping away each rightmost decimal digit of tail, one by one
            (
                (?=(x*)(\3{9}(x*)))  # \3 = floor(tail / 10); \4 = tool to make tail = \3;
                                     # \5 =       tail % 10
                \4                   # tail = \3
            )+                       # Iterate at least once, otherwise \5 could be unset
            # The above loop relies on ECMAScript no-empty-optional behavior. This
            # prevents it from matching when the final result of zero is again divided
            # by 10, which would yield a value of \5 = 0, making it look like one of N's
            # digits was zero and preventing all values of N from matching.
        )
    )
    # \5 is now one of the decimal digits of N, and tail==N again
    # Assert that either of the following two alternatives could match, and in the context
    # outside of the negative lookahead, that neither of them can match:
    (
        (?!\5+$)                     # Assert tail is not divisible by \5
    |                                # or...
        # Assert that the \5 doesn't occur as any decimal digit left of where it was found
        \1                           # tail = \3
        # Chop away any number (minimum 0) of rightmost decimal digits of tail
        (
            (?=(x*)(\8{9}x*))        # \8 = floor(tail / 10); \9 = tool to make tail = \8;
            \9                       # tail = \8
        )*                           # Iterate any number of times, minimum 0
        \5                           # tail -= \5
        (x{10})*$                    # assert tail is divisible by 10
    )
)

Regex (ECMAScript 2018), 85 bytes

^(?!((?=(x*)(\2{9}(x*)))\3)+((?<=(?=(?!\4+$))^.*)|((?=(x*)(\7{9}x*))\8)*\4(x{10})*$))

Try it online!

This is a port of the molecular lookahead version. The most direct port would be replacing (?*A)B with A(?<=(?=B)^.*), but that would result in an 89 byte regex.

^           # tail = N = input number
(?!         # Negative lookahead - Assert that there's no way for the following to match:
    # Loop through chopping away each rightmost decimal digit of tail, one by one
    (
        (?=(x*)(\2{9}(x*)))      # \2 = floor(tail / 10); \3 = tool to make tail = \2;
                                 # \4 =       tail % 10
        \3                       # tail = \2
    )+                           # Iterate at least once, otherwise \4 could be unset
    # The above loop relies on ECMAScript no-empty-optional behavior. This prevents it
    # from matching when the final result of zero is again divided by 10, which would
    # yield a value of \4 = 0, making it look like one of N's digits was zero and
    # preventing all values of N from matching.

    # \4 is now one of the decimal digits of N, and tail==\2
    # Assert that either of the following two alternatives could match, and in the context
    # outside of the negative lookahead, that neither of them can match:
    (
        (?<=                     # Variable-length lookbehind - used to go back to start
            (?=
                # now tail==N again
                (?!\4+$)         # Assert tail is not divisible by \4
            )
            ^.*                  # Go back to start, then execute the above lookahead
        )
    |                            # or...
        # now tail==\2
        # Assert that the \4 doesn't occur as any decimal digit left of where it was found
        # Chop away any number (minimum 0) of rightmost decimal digits of tail
        (
            (?=(x*)(\7{9}x*))    # \7 = floor(tail / 10); \8 = tool to make tail = \7;
            \8                   # tail = \7
        )*                       # Iterate any number of times, minimum 0
        \4                       # tail -= \4
        (x{10})*$                # assert tail is divisible by 10
    )
)
\$\endgroup\$
4
\$\begingroup\$

Scratch, 249 bytes

Try it online!

I feel as though this could be improved by a few bytes, but I'm not sure...

when gf clicked
ask()and wait
set[C v]to()
set[N v]to(answer
repeat(length of(n
change[C v]by(1
add(letter(C)of(N))to[B v
end
say[T
repeat(C
set[D v]to(item(1)of[B v
delete(1)of[B v
if<<[B v]contains(D)?>or<not<((N)/(D))=(round((N)/(D)))>>>then
say(

Alternatively, 34 blocks.

\$\endgroup\$
3
+200
\$\begingroup\$

J, 17 bytes

[:(<.-:~.)10&#.%]

Try it online!

Take input as list of digits.

  • 10&#.%] The list of quotients. Returns floats.
  • (<.-:~.) Does the unique of that list ~. match -: the list formed by rounding every element down to an integer <.?
\$\endgroup\$
2
\$\begingroup\$

Jelly, 8 bytes

D⁼QaDḍ$Ạ

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PHP, 62 48 bytes

while($i=$argn[$k++])$r|=$argn%$i|$$i++;echo!$r;

Run as pipe with -nR or test it online. Empty output for falsy, 1 for truthy.

breakdown

while($i=$argn[$k++])   # loop through digits
    $r|=                    # set $r to true if
        $argn%$i            # 1. number is not divisible by $i
        |$$i++;             # 2. or digit has been used before
echo!$r;                # print negated $r
\$\endgroup\$
2
\$\begingroup\$

Haskell, 61 bytes

(#)=<<show
(d:r)#n=notElem d r&&mod n(read[d])<1&&r#n
_#_=0<3

Try it online!

Defines an anonymous function (#)=<<show which, given a number, returns True or False.

\$\endgroup\$
2
  • \$\begingroup\$ This function fails on the input 10. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 17:17
  • \$\begingroup\$ Sorry, I am wrong about that -- I missed that you're not required to provide any answer for inputs that have 0s in them. \$\endgroup\$
    – CR Drost
    Jul 5, 2017 at 18:02
2
\$\begingroup\$

ruby -n, 40 bytes

p gsub(/./){$'[$&]||$_.to_i%$&.hex}<?0*8

Try it online!

Read in the number as a string. Substitute each character (digit) with a subsequent occurrence of that character, if present, or the whole number modulo that digit. This will result in a string of only 0s if and only if this is a Lynch-Bell number. Why? If there's a repeated digit, every instance of the last stays the same, and since the input doesn't contain zeroes that means a non-zero digit. Otherwise, we're just checking whether every digit evenly divides the number.

Since there are no 8-or-more-digit Lynch-Bell numbers (formal proof: OEIS says so), checking whether the resulting string is lexicographically earlier than the string '00000000' is equivalent to checking whether it's all zeroes.

\$\endgroup\$
2
\$\begingroup\$

Japt, 15 14 11 10 9 bytes

ì®yUÃ¥Uìâ

Try it

\$\endgroup\$
2
  • \$\begingroup\$ ©! -> « for -1 byte \$\endgroup\$ Sep 11, 2017 at 17:35
  • \$\begingroup\$ Thanks, @JustinMariner; don't know how that went so long without being spotted! I found a shorter solution, though. \$\endgroup\$
    – Shaggy
    Sep 11, 2017 at 19:11
2
\$\begingroup\$

Excel, 76 74 bytes

-2 bytes using ROWS instead of COUNTA

=LET(n,LEN(A2),d,MID(A2,SEQUENCE(n),1),AND(ROWS(UNIQUE(d))=n,MOD(A2,d)=0))

Spreadsheet

LET(                               'Assign Variables
n,LEN(A2)                          '   n = # of digits
d,MID(A2,SEQUENCE(n),1)            '   d = array of digits
AND(ROWS(UNIQUE(d))=n,MOD(A2,d)=0) 'Result
    ROWS(UNIQUE(d))=n              '   # unique d = n
                      MOD(A2,d)=0  '   number mod d = 0
AND(                             ) '   and all tests      

Original

=LET(n,LEN(A2),d,MID(A2,SEQUENCE(n),1),AND(COUNTA(UNIQUE(d))=n,MOD(A2,d)=0))
\$\endgroup\$
2
\$\begingroup\$

K (ngn/k), 22 bytes

{&/~(t*t~?t:10\x)!\:x}

Try it online!

  • (...t:10\x) get the individual digits of the input, storing in t
  • (t*t~?t...) if the digits aren't all unique, multiply them by 0 (otherwise leave them as-is)
  • (...)!\:x mod the input by each of the digits (with x mod 0 == x)
  • &/~ are all the remainders 0?
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 57 bitsv2, 7.125 bytes

:₌Kf↔$f⁼

Try it Online!

Bitstring:

010100100010101011101010110011110100011101011011111010110
\$\endgroup\$
2
\$\begingroup\$

Uiua, 13 bytes

≍⊝⌊.÷-@0$"_".

Try it online!

Not too bad, given the bulky syntax to convert a number into its decimal digits. The main idea is taken from alephalpha's Nekomata answer.

Note: the symbol for "match" (test if two arrays are structurally equal as a whole) has been changed from to .

Also, this code doesn't work when the input contains a 0, because division by 0 gives NaN, floor of NaN is still NaN, and two NaNs are equal.

≍⊝⌊.÷-@0$"_".    input: a positive integer
         $"_"     stringify the input
      -@0         subtract '0' from each char, giving decimal digits as numbers
     ÷       .    divide the original number by each digit
≍⊝⌊.             test if this array, floored and deduplicated, still equals itself

A version that also handles zero digits (giving falsy) in 15 bytes (Try it):

≍⊚=0⊃◿⊛-@0$"_".
          -@0$"_"     decimal digits of input
     ⊃◿⊛       .    push (input % digits, classification of digits)
                      (classify: assign a unique index to each unique element)
 ⊚=0                 indices where input % digits == 0
≍                    match?

The final match gives 1 if and only if the number is Lynch-Bell, because:

  • indices where input % digits == 0 has the length equal to the number of digits if and only if input % digits == 0 is all true,
  • its result is precisely 0 1 2 ... x-1 where x is the number of digits in that case, and
  • the classification is the same if and only if the digits are all unique.
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 8 bytes

SÖP¹DÙQ*

Uses the 05AB1E encoding. Try it online!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 57 bytes

Tr@Boole[IntegerQ/@Union[s=#/IntegerDigits@#]]==Length@s&
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can save numerous bytes if you use the builtin IsLynchBellNumber \$\endgroup\$
    – Okx
    Jul 4, 2017 at 14:53
  • 1
    \$\begingroup\$ why don't you make the same offer to Martin? \$\endgroup\$
    – ZaMoC
    Jul 4, 2017 at 15:00
  • \$\begingroup\$ @Okx but it's less fun that way. \$\endgroup\$
    – QBrute
    Jul 4, 2017 at 21:57
  • \$\begingroup\$ @QBrute Can you take a joke? \$\endgroup\$
    – Okx
    Jul 4, 2017 at 22:02
  • 1
    \$\begingroup\$ @Okx It would have been more believable as LynchBellNumberQ. ;) \$\endgroup\$ Jul 5, 2017 at 5:20
1
\$\begingroup\$

Python 2, 66 bytes

This is a solution in Python 2, whose whole purpose is to output True for truthy and False for falsy:

lambda n:len(set(n))==len(n)and not any((int(n)%int(x)for x in n))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Retina, 37 bytes

(.).*\1
0
.
<$&$*1>$_$*
<(1+)>\1+

^$

Try it online! Link includes test cases. Explanation: The first stage replaces any duplicate digit with a zero. The second stage replaces each digit with its unary representation followed by the unary representation of the original number. The third stage then computes the remainder of the division of original number by each non-zero digit. If the number is a Lynch-Bell number then this will delete everything and this is tested for by the final stage.

\$\endgroup\$
1
\$\begingroup\$

Haskell, 260 241 201 162 bytes

f([x])=1<2
f(x:xs)=not(x`elem`xs)&&(f xs)
r n i= n`mod`(read[(show n!!i)]::Int)==0
q n 0=r n 0 
q n i=r n i&&q n(i-1)
p n=length(show n)-1
s n=q n(p n)&&f(show n)

Explanation

f ([x]) = True                                           f function checks for                                                       
f (x:xs) = not(x `elem` xs) && (f xs)                    repeated digits              
r n i = n `mod` (read [(show n !! i)] :: Int) == 0       checks whether num is 
                                                         divisible by i digit
q n 0 = r n 0                                            checks wether n divisible
q n i = r n i && q n (i-1)                               by all of its digits                             
p n = length (show n) -                                  gets length of number                             
s n = (q n (p n)) && (f (show n))                        sums it all up!!!

Have significantly shortened thanx to Laikoni

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG and Haskell golfing in particular! This answer can be considerably shortened by removing superfluous spaces, e.g. the ones around the equal signs or next to parenthesis. \$\endgroup\$
    – Laikoni
    Jul 5, 2017 at 12:40
  • 1
    \$\begingroup\$ You might also be interested in Tips for golfing in Haskell and the Guide to Golfing Rules in Haskell. \$\endgroup\$
    – Laikoni
    Jul 5, 2017 at 12:43
  • \$\begingroup\$ @Laikoni Thanx for your advice! I'm looking into it \$\endgroup\$ Jul 5, 2017 at 13:51
1
\$\begingroup\$

CJam, 17 bytes

CJam is the Java of golfing languages. It's even interpreted in Java!

{_Ab__L|=@@f%:+>}

Explanation:

{   e# Stack:              3915
_   e# Duplicate:          3915 3915
Ab  e# Digits in base 10:  3915 [3 9 1 5]
__  e# Duplicate twice:    3915 [3 9 1 5] [3 9 1 5] [3 9 1 5]
L|  e# Remove duplicates:  3915 [3 9 1 5] [3 9 1 5] [3 9 1 5]
=   e# Test equality:      3915 [3 9 1 5] 1
@@  e# Rotate twice:       1 3915 [3 9 1 5]
f%  e# Modulo each:        1 [0 0 0 0]
:+  e# Sum:                1 0
>   e# Greater than:       1
}   e# Output:             1 (truthy)
\$\endgroup\$
1
\$\begingroup\$

VBScript, 177 bytes

Hey all, this is my very first CG post, and first attempt, so hope I followed all the rules...

Function L(s)
dim i,j,k,m,n
j = Len(s)
redim a(j)
n = 0
for i = 0 to j-1
   A(i) = Mid(s,i+1,1)   
   m = m + s Mod A(i)   
   if j = 1 then         
   else                             
        for k = 0 to i - 1        
            if A(i)  = A(k) then n = n + 1   
        next
   end if
next
if m + n = 0 then L = "y" else L = "n"
End Function

This can be run from Notepad by adding a line at the end

Msgbox L(InputBox(""))

And then saving it as .vbs, then double click.

Explanation:

Function L(s)                  'creates the function "L" taking test number as input
dim i,j,k,t,m,n                'variables
j = Len(s)                     '"j" gets length of test number
redim a(j)                     'creates array "a", size is length of test number 
n = 0                          'sets repeat character counter "n" to zero
for i = 0 to j-1               'for length of string
   A(i) = Mid(s,i+1,1)         'each array slot gets one test number character
   m = m + s Mod A(i)          '"m" accumulates moduli as we test divisibility of each digit
   if j = 1 then               'if test number is of length 1, it passes (do nothing)
   else                        'otherwise, gotta test for repeats     
        for k = 0 to i - 1     'for each digit already in array, test against current digit   
            if A(i)  = A(k) then n = n + 1  
                               'repeat char counter "n" stores no of repeats  
        next                   'proceed through array looking for repeat  
   end if
next                           'test next digit for divisibility and repeats
if m + n = 0 then L = "y" else L = "n"      
                               'check for any repeats and moduli,
                               'then return yes or no for LynchBelledness
End Function

VBScript is a bit of blunt instrument for golfing, but hey, I haven't learned Ruby yet...

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can't you remove some of the whitespace like 'L="y"' \$\endgroup\$
    – Okx
    Jul 8, 2017 at 20:01
  • \$\begingroup\$ Technically, yes! I shoulda done that...btw, i'm looking at codegolf languages that might be cool to learn, but for most, the documentation is minimal to non-existent...can anyone recommend a good language that is well documented? Was trying out "Actually/Seriously" but hit some obstacles due to lack of doc.... \$\endgroup\$
    – aAaa aAaa
    Jul 11, 2017 at 0:07
1
\$\begingroup\$

Pyth, 10 bytes

qiRQKjQT{K

Verify all the test cases.

How?

qiRQKjQT{K  ~ Full program.

     jQT    ~ The list of decimal digits of the input.
    K       ~ Assign to a variable K.
 iRQ        ~ For each decimal digit...
 i Q          ~ ... Get the greatest common divisor with the input itself.
        {K  ~ K with the duplicate elements removed.
q           ~ Is equal? Output implicitly.

Pyth, 11 bytes

&!f%sQsTQ{I

Verify all the test cases.

How?

&!f%sQsTQ{I  ~ Full program, with implicit input.

  f     Q    ~ Filter over the input string.
   %sQsT     ~ The input converted to an integer modulo the current digit.
             ~ Keeps it if it is higher than 0, and discards it otherwise.
 !           ~ Negation. If the list is empty, returns True, else False.
&        {I  ~ And is the input invariant under deduplicating? Output implicitly.
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 24 bytes

{((,⍵)≡∪⍵)×∧/0=(⍎¨⍵)|⍎⍵}

Try it online!

Simple Dfn, can probably be golfed a bit more. Yield standard APL booleans 1 for truthy, 0 for falsy.

It's worth to mention that the function takes the arguments as strings rather than ints.

How:

{((,⍵)≡∪⍵)×∧/0=(⍎¨⍵)|⍎⍵} ⍝ Dfn, argument ⍵.
                      ⍎⍵  ⍝ Convert ⍵ to integer
                     |    ⍝ Modulus
                (⍎¨⍵)     ⍝ Each digit in ⍵
              0=          ⍝ Equals 0? Returns a vector of booleans
            ∧/            ⍝ Logical AND reduction
           ×              ⍝ multiplied by (the result of)
  (     ∪⍵)               ⍝ unique elements of ⍵
       ≡                  ⍝ Match
   (,⍵)                   ⍝ ⍵ as a vector; the Match function then returns 1 iff all digits in ⍵ are unique
\$\endgroup\$
1
  • \$\begingroup\$ (,⍵)≡∪(,=∪) \$\endgroup\$
    – Razetime
    Mar 22, 2021 at 3:47
1
\$\begingroup\$

Python 3, 50 bytes

lambda n:all(int(n)%int(d)+n.count(d)<2for d in n)

Try it online!

Returns True if the (zero-free) number is a Lynch-Bell number, False otherwise.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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