20
\$\begingroup\$

NOTE: Some terminology used in this challenge is fake.

For two integers n and k both greater than or equal to 2 with n > k, n is semidivisible by k if and only if n/k = r/10 for some integer r. However, n may not be divisible by k. Put more simply, the base 10 representation of n/k has exactly one digit after the decimal place. For example, 6 is semidivisible by 4 because 6/4=15/10, but 8 is not semidivisible by 4 because 8 % 4 == 0.

Your task is to write a program which takes in two integers as input, in any convenient format, and outputs a truthy (respectively falsy) value if the first input is semidivisible by the second, and a falsey (respectively truthy) value otherwise. Standard loopholes are forbidden. You may assume that n > k and that both n and k are at least 2.

Test cases:

[8, 4] -> falsey
[8, 5] -> truthy
[9, 5] -> truthy
[7, 3] -> falsey

This question is therefore shortest answer in bytes wins.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Do our chosen outputs for truthy & falsey need to be consistent? \$\endgroup\$ – Shaggy Mar 6 at 23:44
  • \$\begingroup\$ @Shaggy not necessarily \$\endgroup\$ – Recursive Co. Mar 7 at 7:50

28 Answers 28

14
\$\begingroup\$

Python 2, 24 bytes

lambda n,k:n*10%k==0<n%k

Try it online!

Checks that \$10n\$ is a multiple of \$k\$, but \$n\$ itself is not.

24 bytes

lambda n,k:n%k>>n*10%k*n

Try it online!

The *n at the end can't be omitted, say for n=19001, k=10000.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 23 bytes? \$\endgroup\$ – dingledooper Mar 6 at 19:12
  • \$\begingroup\$ This is what I was attempting to golf @dingledooper, it should be good if it expands to 1>n*10%k AND n*10%k <n%k as I think \$\endgroup\$ – AZTECCO Mar 7 at 0:00
  • 1
    \$\begingroup\$ @dingledooper Yes... Post an answer and I'll give it a bounty. \$\endgroup\$ – xnor Mar 7 at 23:37
  • \$\begingroup\$ @xnor Posted \$\endgroup\$ – dingledooper Mar 8 at 5:30
10
\$\begingroup\$

R, 31 bytes

function(x,y)nchar((x/y)%%1)==3

Try it online!

Fractional part (%%1) of x/y must be 3 characters: so, 0.1, 0.2 ... 0.9, but not 0 or 0.3333.

\$\endgroup\$
9
+100
\$\begingroup\$

Python 2, 23 bytes

Saves 1 byte from xnor's answer (for the bounty). Notice that the chained comparison forces n*10%k = 0 and n%k > 0 to both be true.

lambda n,k:1>n*10%k<n%k

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I'll post the bounty when the question is old enough to allow it \$\endgroup\$ – xnor Mar 8 at 11:36
8
\$\begingroup\$

APL (Dyalog Unicode), 12 9 bytes

3=∘≢∘⍕1|÷

Try it online!

The previous, obvious method.

'.'=∘⊃¯2↑∘⍕÷

Try it online!

\$\endgroup\$
1
  • 6
    \$\begingroup\$ It works, but it's not so obvious to me ;) \$\endgroup\$ – Recursive Co. Mar 6 at 13:28
6
\$\begingroup\$

Japt, 10 bytes

I think this is right; I'm quite drunk!

*A vV «UvV

Try it

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

C (gcc), 25 24 23 bytes

Saved a byte thanks to AZTECCO!!!
Thanks to dingledooper who found and fixed a bug!!!

f(n,k){n=n%k>0>10*n%k;}

Try it online!

Inputs integers \$n\$ and \$k\$ and returns a truthy iff \$10n\$ is divisible by \$k\$ and \$n\$ isn't divisible by \$k\$.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ <1 instead of ==0 \$\endgroup\$ – AZTECCO Mar 6 at 16:15
  • \$\begingroup\$ @AZTECCO Nice one - thanks! :D \$\endgroup\$ – Noodle9 Mar 6 at 16:18
  • \$\begingroup\$ This version doesn't work, unfortunately, since 10*n%k does not have to be non-zero. For example for 7 4 it returns 1. \$\endgroup\$ – dingledooper Mar 7 at 0:10
  • 2
    \$\begingroup\$ Ah I see why it fails now, the order of precedence is wrong. I think switching it works? Try it online!. \$\endgroup\$ – dingledooper Mar 7 at 1:11
  • 1
    \$\begingroup\$ @dingledooper We definitely need testcases like that - nice one, thanks! :D \$\endgroup\$ – Noodle9 Mar 7 at 11:56
4
\$\begingroup\$

Haskell, 30 bytes

n#k=(10*n`mod`k<1)>(n`mod`k<1)

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript (Node.js), 18 bytes

n=>k=>n%k>0>10*n%k

Try it online!

-2 bytes thanks to iota

-1 byte indirectly from Neil or Noodle9 or dingledooper

\$\endgroup\$
12
  • 1
    \$\begingroup\$ fails for [1,7]. You need to check if n*10%k is 0. \$\endgroup\$ – ovs Mar 6 at 15:05
  • \$\begingroup\$ @ovs we have to take n > k with k > 1. \$\endgroup\$ – Sheik Yerbouti Mar 6 at 15:13
  • 1
    \$\begingroup\$ I didn't read the entire challenge spec before, but this still fails for [8,7] \$\endgroup\$ – ovs Mar 6 at 15:17
  • \$\begingroup\$ @ovs oh I get it, I was thinking that n%k returns a bool telling whether n is divisible by k Try it Online!. I don't know why I had this confusion. \$\endgroup\$ – Sheik Yerbouti Mar 6 at 15:26
  • 1
    \$\begingroup\$ 18 bytes and returns a boolean \$\endgroup\$ – user81655 Mar 7 at 0:45
4
\$\begingroup\$

Husk, 8 bytes

€tḊ10/¹⌉

Try it online!

Uses a different approach from what I have seen in other answers, could be even shorter if I found a better way to check if a number is in [2,5,10]. (Any golfing language with a single-byte builtin for 10 could probably do it)

Explanation

Taking 2 numbers \$a\$ and \$b\$, we compute: $$\frac{lcm(a,b)}{a}$$

This value will be equal to the smallest number you have to multiply \$a\$ by to make it divisible by \$b\$. Since we don't want \$a\$ to be divisible by \$b\$ we'll need this to be greater than 1, and since we want \$10*a\$ to be divisible by \$b\$ we'll need this to be a divisor of 10. In the end we want the result to be one of [2,5,10].

€tḊ10/¹⌉
       ⌉    Least common multiplier of the two numbers
     /¹     divided by the first number
€           Find its position in the list: (returns 0 if missing)
  Ḋ10        divisors of ten: [1,2,5,10]
 t           except the first: [2,5,10]
\$\endgroup\$
3
  • \$\begingroup\$ I assume İ€ doesn't help here? \$\endgroup\$ – Razetime Mar 9 at 3:29
  • \$\begingroup\$ @Razetime unfortunately not, numbers like 100 would be a problem \$\endgroup\$ – Leo Mar 9 at 8:44
  • 1
    \$\begingroup\$ other golfing languages would have a single byte 10, but a two-byte divisors :P \$\endgroup\$ – Razetime Mar 9 at 9:29
3
\$\begingroup\$

J, 11 bytes

3=[#@":@%~|

Try it online!

Looks like I stumbled on almost exactly the same method as the APL answer.

  • [...%~| The left input [ floating point divided into %~ the remainder when the left input is divided into the right input |.
  • #@":@ Format that result as a string and take its length.
  • 3= Does that equal 3? This will only be true for numbers of the form 0.n where n is an element of the digits 1-9.
\$\endgroup\$
3
\$\begingroup\$

Jelly, 6 bytes

×⁵ọ¹ȧ%

Try it online!

\$\endgroup\$
3
\$\begingroup\$

GolfScript, 15 bytes

.~@~%!!@10*@%!&

Try it online!

Unfortunately, as GolfScript lacks support for floating-point values, I couldn't use the strategy of looking for the decimal place. The program just checks if n % k is truthy and 10n % k is falsy.

Also of note is that I found it easier to take the input as a string containing two space-separated integers instead of taking it as two integers directly.

.~@~                  prepare two sets of n and k
    %!!               check if n % k > 0
       @10*           multiply n by 10
           @%!        check if 10n % k = 0
              &       AND both values
\$\endgroup\$
3
\$\begingroup\$

R, 28 bytes

function(x,y)!(x*10)%%y&x%%y

Try it online!

Test harness taken from Dominic's answer.

\$\endgroup\$
3
\$\begingroup\$

Excel, 31 29 20 bytes

=LEN(MOD(A1/B1,1))=3

-2 bytes thanks to @Dominic van Essen

-9 bytes porting Dominic's Answer

Previous Answer

=MOD(A1,B1)*(MOD(A1*10,B1)=0)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice. I think you can shave-off the >0 because non-zero numeric values are truthy in Excel (try: =IF(A1,"TRUTHY","FALSY")... \$\endgroup\$ – Dominic van Essen Mar 7 at 14:28
2
\$\begingroup\$

Retina 0.8.2, 41 bytes

\d+
$*
A`^(1+),\1+$
r`1\G
10$*
^(1+),\1+$

Try it online! Takes inputs in reverse order, but header in link reverses the test suite for convenience. Explanation:

\d+
$*

Convert to decimal.

A`^(1+),\1+$

Ensure nondivisibility.

r`1\G
10$*

Multiply only the dividend by 10.

^(1+),\1+$

Ensure divisibility.

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

Charcoal, 9 bytes

⁼¹⌕⮌I∕NN.

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for semidivisble, nothing if not. Explanation:

      N     First input
     ∕      Divided by
       N    Second input
    I       Cast to string
   ⮌        Reversed
  ⌕         Index of
        .   Literal `.`
⁼           Equal to
 ¹          Literal 1
            Implicitly print
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 36 bytes

param($n,$k)!((10*$n)%$k)-and($n%$k)

Try it online!

Inspired by @xnor method of checking if 10n is multiple of k but n itself not

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

Husk, 9 bytes

&¬%³*10¹%

Try it online!

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

K (ngn/k), 13 bytes

{>/~y!10 1*x}

Try it online!

Uses @xnor's approach. n is x, and k is y.

  • 10 1*x generate list of 10 times x, and x
  • y! mod each of those by y
  • ~ not them, i.e. check if y evenly divides 10*x and x
  • >/ return true iif y evenly divides 10*x but not x
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 9 8 8 6 bytes

-2 thanks to @Kevin Cruijssen

/`×'.å

Try it online!

Try more cases

\$\endgroup\$
4
  • \$\begingroup\$ 7 bytes \$\endgroup\$ – lyxal Mar 9 at 10:14
  • \$\begingroup\$ @Lyxal that returns 1 on [8, 4] even though it should be falsely \$\endgroup\$ – Command Master Mar 9 at 10:16
  • \$\begingroup\$ Also generally 2(è can be turned to ¨θ \$\endgroup\$ – Command Master Mar 9 at 10:38
  • 1
    \$\begingroup\$ /`×'.å (6 bytes) \$\endgroup\$ – Kevin Cruijssen Mar 30 at 10:58
2
\$\begingroup\$

Vyxal, 6 bytes

/Ṫt\.=

Try it Online!

Checks if the second last character is "."

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

Raku, 16 bytes

*/(.1&none 1)%%*

Try it online!

This is an anonymous function where the asterisks represent the two arguments. The main expression is * / X %% *, which checks that the first argument divided by an expression X is divisible by the second argument. X here is an and-junction of the number .1, and the none-junction of the number 1. Raku threads the expression over the junction, producing a truthy value if the first argument divided by .1 is divisble by the second argument, AND the first argument divided by 1 is NOT divisible by the second argument.

The return value is a junction of boolean values, which collapses into a single value in a boolean context. Fortunately, this challenge did not stipulate that the return value must be one of two distinct values, or I'd have to add a so to collapse the junction to a regular boolean, for two more bytes.

Note that I used division instead of multiplication because an extra space would have been required to separate the first function argument from the multiplication operator: * *(10&none 1)%%*. .1 is a rational number in Raku, not a floating-point number, so there's no danger of floating-point rounding error.

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

Java, 35 bytes

n->k->(n/k+"").matches(".+\\.[^0]")

Port of my JavaScript answer.

Try it online!

Java, 20 bytes

n->k->n*10%k<1&n%k>0

Port of xnor's answer.

Try it online!

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

JavaScript, 22 bytes

n=>k=>/\..$/.test(n/k)

Checks that there is exactly one decimal place after division.

Try it online!

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

Scala, 20 bytes

n=>k=>n%k-n*10%k*k>0

Try it in Scastie

If n is divisible by k but not n*10, n%k-n*10%k*k is negative. If the both are divisible by k, it's 0. If only n*10 is divisible by k, it's positive. If neither is divisible by k, it's still negative, because we're multiplying the second by k to make it bigger.

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

Templates Considered Harmful, 51 bytes

Fun<If<Rem<Mul<I<10>,A<1>>,A<2>>,F,Rem<A<1>,A<2>>>>

Try it online!

Implementation of xnor's answer. Anonymous function that takes 2 arguments.

Fun<
    If<Rem<Mul<I<10>,A<1>>,A<2>>,  # if 10n%k > 0
       F,                          # false
       Rem<A<1>,A<2>>>>            # else n%k
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes

{≠/0=1|q,10×q←⍺÷⍵}

Not as clever as this solution, but whatcha gonna do... :shrug:

≠/ ⍝ fold by way of not equals (xor)
  0=1| ⍝ divisibility check
      q,10×q ⍝ concatenate 
           q←⍺÷⍵ ⍝ assignment

Try it online!

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

VBScript, 45 bytes

sub f(n,k)
msgbox 10*n mod k<>n mod k
end sub

Porting of @xnor's answer a little changed.

Check if 10n is multiple of k but n itself not by checking those conditions aren't equal with <>

\$\endgroup\$

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.