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

1
2
1
\$\begingroup\$

Perl 5 -p, 34 29 bytes

$_=!grep$k{$_}++|"@F"%$_,/./g

Try it online!

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

R, 86 bytes

x=scan(,'');a=as.double;all(table(utf8ToInt(x))==1)&&all(!a(x)%%a(el(strsplit(x,""))))

Takes input as a string. I certainly feel like this is golfable.

Try it online!

R, 62 bytes

Golf by Giuseppe

all(table(d<-utf8ToInt(x<-scan(,''))-48)<2)&all(!strtoi(x)%%d)

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 63 bytes. \$\endgroup\$
    – Giuseppe
    Mar 23, 2021 at 18:53
  • \$\begingroup\$ @Giuseppe How you do it I'll never know... \$\endgroup\$
    – Sumner18
    Mar 23, 2021 at 20:01
  • \$\begingroup\$ Practice, haha. I started with minor golfs like substituting strtoi for as.double, then noticed that digits were being extracted twice (once with utf8ToInt and once with strsplit), so I got them once at the beginning (with strsplit), then tried to get rid of the strtoi entirely by using utf8ToInt()-48 (which I didn't notice before, but that's also shorter than strsplit), but I wasn't able to get it down all the way. \$\endgroup\$
    – Giuseppe
    Mar 23, 2021 at 20:51
  • \$\begingroup\$ Another failed approach was to try to collapse the all(...)&all(...) into one all(...&...), which errored out. I did manage to find 62 bytes by better in-lining of assignments, though, so that's neat. \$\endgroup\$
    – Giuseppe
    Mar 23, 2021 at 20:53
1
\$\begingroup\$

Nekomata + -e, 3 bytes

Ɗ¦ů

Attempt This Online!

Ɗ¦ů
Ɗ       Decimal digits
 ¦      Divided by, and check if the result are integers
  ů     Check if all elements are unique
\$\endgroup\$
5
  • \$\begingroup\$ Q: Is there a way that you could adjust this so it doesn't need a flag? Or is the existence/nonexistence of the non-deterministic solutions 'invisible' from within the program? \$\endgroup\$ Oct 20, 2023 at 15:41
  • \$\begingroup\$ @DominicvanEssen Nekomata doesn't have truthy/falsy. so the flag -e is needed for every decision-problem challenge. \$\endgroup\$
    – alephalpha
    Oct 21, 2023 at 0:12
  • \$\begingroup\$ @DominicvanEssen You can add an n (\countValue) to the end of the code so that it returns 1 and 0 (Attempt This Online!). But they are not truthy/falsy by this definition on meta. \$\endgroup\$
    – alephalpha
    Oct 21, 2023 at 0:20
  • 1
    \$\begingroup\$ Surely 'non-fail' and 'fail' could be considered to be analogous to 'truthy' and 'falsy'? So andThen behaves as "if X doesn't fail, then ..." like this, and if behaves as "if X fails, then ..." like this... \$\endgroup\$ Oct 21, 2023 at 8:00
  • 1
    \$\begingroup\$ (in which case, Ɗ¦ů is already valid as a decision function as-is, returning 'non-fail' or 'fail', and the -e flag is just for pretty output, and not really required...) \$\endgroup\$ Oct 21, 2023 at 8:02
0
\$\begingroup\$

Neim, 9 bytes

𝐮ℚ₁𝐅₁𝕌₁ℚ𝕒

Try it online!

-2 thanks to Okx.

Hmm, there's a nice symmetry... oO.O.O.Oo

\$\endgroup\$
3
  • \$\begingroup\$ You can golf 𝐂D𝐮𝔼 to 𝐮ℚ (uniquify, check for equality ignoring types) \$\endgroup\$
    – Okx
    Jul 4, 2017 at 12:11
  • \$\begingroup\$ Still room for golfing there ;) \$\endgroup\$
    – Okx
    Jul 4, 2017 at 12:19
  • \$\begingroup\$ @Okx Hmm...I don't see anything particularly interesting there. \$\endgroup\$ Jul 4, 2017 at 12:26
0
\$\begingroup\$

Perl 6, 27 bytes

{$_%%.comb.all&&[!=] .comb}

Try it online!

  • .comb is a method which, when given no arguments, splits a string into its individual characters. A number is implicitly converted into a string, and so .comb returns its digits.
  • .comb.all is an and-junction of all of the digits.
  • $_ %% .comb.all is an and-junction of the divisibility of the input argument $_ by all of its digits. For example, if $_ is 123, the junction is all(True, False, True), which collapses to False in a truthy context.
  • [!=] .comb reduces the digits of the input argument with the != operator, which evaluates to True if the digits are all different.
\$\endgroup\$
0
\$\begingroup\$

Ruby 2.4, 42 bytes

->x{(r=x.digits)|[]==r&&r.none?{|f|x%f>0}}

(No TIO yet, sorry)

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

PHP, 51 bytes

prints zero for true and one for false

for(;$c=$argn[$i++];$$c=1)$t|=$$c||$argn%$c;echo$t;

Try it online!

PHP, 62 bytes

prints zero for true and one for false

for($a=$argn;$c=$a[$i];)$t|=strpos($a,$c)<+$i++||$a%$c;echo$t;

Try it online!

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

Kotlin 1.1, 98 66 59 bytes

{i->i.none{i.toInt()%(it-'0')>0}&&i.length==i.toSet().size}

Beautified

{i ->
    // None of the digits are not factors
    i.none { i.toInt() % (it-'0') > 0 }
    // AND
    &&
    // None of the digits are repeated
    i.length == i.toSet().size
}

Test

var L:(String)-> Boolean =
{i->i.none{i.toInt()%(it-'0')>0}&&i.length==i.toSet().size}
data class TestData(val input: String, val output: Boolean)

fun main(args: Array<String>) {
    var inputs = listOf(
        TestData("7", true),
        TestData("126", true),
        TestData("54", false),
        TestData("55", false),
        TestData("3915", true)
    )

    for (test in inputs) {
        if (L(test.input) != test.output) {
            throw AssertionError(test.toString())
        }
    }
    println("Test Passed")
}
\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 39 bytes

f(x,d=digits(x))=rem.(x,d)==0*unique(d)

rem.(x,d) is a vector containing the remainders after dividing x by each digit in x. 0*unique(d) is a vector with length equal to the number of unique digits, with all zero values. Check if they are equal.

Try it online!

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

PowerShell, 95 bytes

param($n)$a="$n"|% t*y;if($n-eq([int](($a|gu)-join''))){
($a|%{12%[int]"$_"})-contains0}else{0}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 54 and 1232 should be falsy \$\endgroup\$
    – mazzy
    Apr 10, 2021 at 11:31
0
\$\begingroup\$

APL (Dyalog Unicode), 31 bytes

{∧/(0=⍺|⍨¨⍵)∧(⊢≡∪)⍵}∘(10⊥⍣¯1⊢)⍨
 ∧/ ⍝ reduction by logical AND
   0=⍺|⍨¨⍵ ⍝ divisibility check
     ∧ ⍝ logical AND
       (⊢≡∪)⍵ ⍝ check for unique digits
         ∘ ⍝ function composition operator (Beside)
           10⊥⍣¯1⊢ ⍝ convert to array of digits
             ⍨ ⍝ commute operator (Selfie), used to copy input to left side of the composed function

Try it online!

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

Desmos, 109 bytes

n=q-48
l=n.length
abs(\prod_{a=1}^l\prod_{b=a+1}^{l}(n[b]-n[a])(1-sign(mod(\sum_{c=1}^ln[c]10^{l-c},n).max)))

View it in Desmos

There's got to be a better way to do this, but I can't come up with it right now. This is sort of split up into a few sections:

n=q-48

This turns codepoints into digits for convenience's sake

\prod_{a=1}^l\prod_{b=a+1}^{l}(n[b]-n[a])

This checks if any two digits are equal, and if so, returns 0

\sum_{c=1}^ln[c]10^{l-c}

This turns the list of digits into the number

1-sign(mod(\sum_{c=1}^ln[c]10^{l-c},n).max)

This uses the previous section to check if any digit isn't divisible by the sum, and if so, returns 0

Then we turn this into a 1 if the result succeeds both tests and 0 if it fails one. We can cut out 5 bytes if we accept any non-zero number as true by removing the abs( from the start of the third equation and ) from the end, but I'm not sure if negative numbers really count as truthy. We can also get rid of 6 bytes if we can accept a list of digits as input rather than a string.

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

Python 3, 88 86 bytes

def f(n):
 s=""
 for i in n:
  if i in s or int(n)%int(i)!=0:return 0
  s+=i
 return 1

Fixed that it wasn't running with whitespace removed - thanks to @caird coinheringaahing

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to Code Golf! Your version without whitespace doesn't work, but it's an easy fix and golf \$\endgroup\$ Apr 10, 2021 at 20:40
  • \$\begingroup\$ @cairdcoinheringaahing Thanks for checking, is there a syntax guide anywhere for writing functions without whitespace without messing them up and having them not run? \$\endgroup\$ Apr 10, 2021 at 20:49
  • \$\begingroup\$ Not sure about a syntax guide but feel free to check out our New users guide for golfing in Python for more info \$\endgroup\$ Apr 10, 2021 at 20:50
  • \$\begingroup\$ @cairdcoinheringaahing Okay thanks. What actually is it about the syntax that isn't allowed? \$\endgroup\$ Apr 10, 2021 at 20:52
  • 1
    \$\begingroup\$ So long as the program runs as is, it's a valid submission. As the program doesn't run when you remove the newlines, it isn't a valid submission \$\endgroup\$ Apr 10, 2021 at 20:53
0
\$\begingroup\$

JavaScript (Node.js), 33 bytes

s=>[...s].every(y=e=>y[e]^=s%e<1)

Try it online!

If an e appears twice then s%e<1 is still true, and y[e]^= returns 0

\$\endgroup\$
1
2

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.