# Semidivisibility

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.

• Do our chosen outputs for truthy & falsey need to be consistent? Mar 6, 2021 at 23:44
• @Shaggy not necessarily
– user100690
Mar 7, 2021 at 7:50

# 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.

• 23 bytes? Mar 6, 2021 at 19:12
• 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 Mar 7, 2021 at 0:00
• @dingledooper Yes... Post an answer and I'll give it a bounty.
– xnor
Mar 7, 2021 at 23:37
• @xnor Posted Mar 8, 2021 at 5:30

# 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.

# 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!

• I'll post the bounty when the question is old enough to allow it
– xnor
Mar 8, 2021 at 11:36

# APL (Dyalog Unicode), 12 9 bytes

3=∘≢∘⍕1|÷


Try it online!

The previous, obvious method.

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


Try it online!

• It works, but it's not so obvious to me ;)
– user100690
Mar 6, 2021 at 13:28

# Japt, 10 bytes

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

*A vV «UvV


Try it

# 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\$$.

• <1 instead of ==0 Mar 6, 2021 at 16:15
• @AZTECCO Nice one - thanks! :D Mar 6, 2021 at 16:18
• 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. Mar 7, 2021 at 0:10
• Ah I see why it fails now, the order of precedence is wrong. I think switching it works? Try it online!. Mar 7, 2021 at 1:11
• @dingledooper We definitely need testcases like that - nice one, thanks! :D Mar 7, 2021 at 11:56

# 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]

• I assume İ€ doesn't help here? Mar 9, 2021 at 3:29
• @Razetime unfortunately not, numbers like 100 would be a problem
– Leo
Mar 9, 2021 at 8:44
• other golfing languages would have a single byte 10, but a two-byte divisors :P Mar 9, 2021 at 9:29

n#k=(10*nmodk<1)>(nmodk<1)


Try it online!

# Jelly, 6 bytes

×⁵ọ¹ȧ%


Try it online!

# 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

• fails for [1,7]. You need to check if n*10%k is 0.
– ovs
Mar 6, 2021 at 15:05
• @ovs we have to take n > k with k > 1. Mar 6, 2021 at 15:13
• I didn't read the entire challenge spec before, but this still fails for [8,7]
– ovs
Mar 6, 2021 at 15:17
• @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. Mar 6, 2021 at 15:26
• 18 bytes and returns a boolean Mar 7, 2021 at 0:45

# 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.

# 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

• 31 bytes using truthey / falsey values Jul 3, 2022 at 20:35

# 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


# R, 28 bytes

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


Try it online!

Test harness taken from Dominic's answer.

# Excel, 3129 20 bytes

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


-2 bytes thanks to @Dominic van Essen

=MOD(A1,B1)*(MOD(A1*10,B1)=0)

• Nice. I think you can shave-off the >0 because non-zero numeric values are truthy in Excel (try: =IF(A1,"TRUTHY","FALSY")... Mar 7, 2021 at 14:28

# Retina 0.8.2, 4140 35 bytes

$0 \d+$*
^(1+),(?!(\1{10})+$)\1+$


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

$0  Multiply n by 10. \d+$*


Convert k and 10n to unary.

^(1+),


Match k, then ...

(?!(\1{10})+$)  ... while ensuring 10k doesn't divide 10n (i.e k does not divide n), ... \1+$


... ensure k divides 10n.

• Got my answer down to 40 bytes in Retina. Jul 3, 2022 at 3:05
• @Deadcode Nice, so that means we're tied now ;-)
– Neil
Jul 3, 2022 at 7:15
• Cool. I figured you'd probably be able to get it down by at least 1 byte once the necessity arose. :-) Jul 3, 2022 at 7:22
• @Deadcode It turns out that I was able to get it down by at least 1 byte. Or more accurately, once I'd come up with the alternative approach, I was then able to golf that down further.
– Neil
Jul 3, 2022 at 8:47
• Nicely done, now that's more like it. This of course doesn't mean that yours wins in all regards; mine is still competitive as a pure regex. Jul 3, 2022 at 8:55

# Vyxal v2.6.0+, 6 bytes

ġ/₀KḢc


Takes its input as $$\k\$$ followed by $$\n\$$.

Try it Online!

This uses the same algorithm as my regex answer, which is similar to the algorithm used by Leo's Husk answer. It asserts that $${k\over gcd(n,k)} ∈ [2,5,10]$$

It doesn't use floating point, and can thus handle arbitrarily large integers correctly.

ġ            Push gcd(n,k)
/           Pop the above result, and push k divided by it
₀K         Push the divisors of 10, i.e. [1, 2, 5, 10]
Ḣ        Head remove – drop first element, yielding [2, 5, 10]
c       Is the above quotient contained in the above list?


## Vyxal v2.5.3ω, 7 bytes

ġ/₀Kḣ„c


Takes its input as $$\k\$$ followed by $$\n\$$.

Try it Online! - v2.5.3ω
Try it Online! - latest

The reason I didn't use the Ḣ head-remove element in the first place is that I was referring to outdated documentation – my clone of the git repo was pointed at master instead of main.

ġ             Push gcd(n,k)
/            Pop the above result, and push k divided by it
₀K          Push the divisors of 10, i.e. [1, 2, 5, 10]
ḣ         Head extract – split the above, yielding 1 and [2, 5, 10]
„        Rotate stack left, such that a=the above quotient, and b=[2, 5, 10]
c       Is a contained in b?


# Vyxal v2.5.3ω, 7 bytes

Ḋ⁰₀*¹Ḋ<


Takes its input as $$\k\$$ followed by $$\n\$$.

Try it Online! - v2.5.3ω
Try it Online! - latest

This uses the same logic as in xnor's Python answer and many subsequent answers, which is to assert that $$\n≢0\pmod k\ \land\ 10n≡0\pmod k\$$.

Ḋ             Push 1 if n is divisible by k, 0 otherwise
⁰            Push n
₀           Push 10
*          Push n*10, popping both n and 10
¹         Push k
Ḋ        Push 1 if n*10 is divisible by k, 0 otherwise (pop both)
<       Is the top boolean less than the bottom boolean?


## Vyxal v2.5.3ω, 7 bytes, 6 elements

k≈↵*⁰Ḋ¯


Takes its input as $$\n\$$ followed by $$\k\$$.

Try it Online! - v2.5.3ω
Try it Online! - latest

k≈            Push [0, 1]
↵           Raise 10 to the power of the above: [1, 10]
*          Multiply the above list by n
⁰         Push k
Ḋ        Are the list items divisible by k? (Result is a list)
¯       Take deltas (consecutive differences) of the list. Iff the two
elements of the list were different, this will be truthy.


Oddly, the behavior of ¯ was changed. It used to give $$\[a-a, a-a, …]\$$ but now gives $$\[a-a, a-a, …]\$$. This doesn't change the truthiness of the output, just the sign of the truthy result: ⟨-1⟩ in old, ⟨1⟩ in new.

• Why not use the latest version of Vyxal? lyxal.pythonanywhere.com is really outdated Jul 2, 2022 at 0:17
• @Steffan Ah, thanks. I didn't realize there was more than one interpreter link. Jul 2, 2022 at 0:35

# Charcoal, 9 bytes

⁼¹⌕⮌Ｉ∕ＮＮ.


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

      Ｎ     First input
∕      Divided by
Ｎ    Second input
Ｉ       Cast to string
⮌        Reversed
⌕         Index of
.   Literal .
⁼           Equal to
¹          Literal 1
Implicitly print


# Husk, 9 bytes

&¬%³*10¹%


Try it online!

# 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

# 05AB1E, 988 6 bytes

-2 thanks to @Kevin Cruijssen

/×'.å


Try it online!

Try more cases

• 7 bytes Mar 9, 2021 at 10:14
• @Lyxal that returns 1 on [8, 4] even though it should be falsely Mar 9, 2021 at 10:16
• Also generally 2(è can be turned to ¨θ Mar 9, 2021 at 10:38
• /×'.å (6 bytes) Mar 30, 2021 at 10:58

# Vyxal 2.0.0, 6 bytes

/Ṫt\.=


Try it Online!

Checks if the second last character is "."

• This crashes when n%k==0, e.g. with the test case [8, 4]. Did it work on an earlier version of Vyxal? Jul 1, 2022 at 23:48
• @Steffan It crashes on a different set of cases in the latest version. Jul 2, 2022 at 1:21
• Here's a 6 byter that works in all cases (as long as the numbers aren't too large) using the same basic algorithm. It returns falsey for semidivisible. Jul 3, 2022 at 8:20
• @Steffan P.S. I think an algorithm like this is basically impossible to implement in modern Vyxal, due to its automatic approximation of floats in fractional notation. I couldn't find any way to disable this behavior in the latest version of Vyxal; how about implementing one, perhaps with a flag? Jul 3, 2022 at 16:58

# APL, 14 bytes

{0≠.=⍵|1 10×⍺}


Explanations. For example, ⍺ = 3 and ⍵ = 5

1 10×⍺ ⍝ Make two-element vector: 3 30
⍵|     ⍝ Remanders of division to 5: 3 0
0≠.=   ⍝ Check if they are zero: 0 1 (no yes)
⍝ Then check if answers are different


# Regex (ECMAScript), 35 33 bytes

^(?=(.+)\1*,\1+$)(\1\B|\1{5})\2?,  Takes its input in unary, as the length of two strings of xs delimited by a , specifying ($$\k,n\$$). Try it online! This uses a similar algorithm to Leo's Husk answer. We can't directly compute $$\lcm(n,k)\$$ since it can be larger than both the inputs, so instead we compute $$\k/gcd(n,k)\$$, which is conveniently equivalent to $$\lcm(n,k)/n\$$. All that remains is to assert that the result is in $$\[2,5,10]\$$. ^ # tail = K (?= (x+)\1*,\1+$    # \1 = greatest common factor of K and N
)

# Assert that K == \1*2, \1*5, or \1*10, i.e. that K/\1 == 2, 5, or 10
(                   # \2 = one of the below choices, \1 or \1*5
\1              # tail -= \1
\B              # Assert tail > 0; this prevents matching on K == \1
|                   # or
\1{5}           # tail -= \1*5
)
\2?                 # optionally, tail -= \2; this option must be taken if
# \2 == \1, because in that choice, the assertion was made
# that K > \1
,                   # Assert tail == 0


# Regex (ECMAScript), 33 bytes

^(((x+)(\3{4})?)\2?),(?!\1+$)\3+$


Try it online!

By an interesting coincidence, there's another method that has the same length. It asserts that both:

• $$\k\$$ does not divide $$\n\$$
• At least one member of $$\\{k, {k\over 2}, {k\over 5}, {k\over 10}\}\$$ is an integer and divides $$\n\$$ (of course it's redundant to try to assert that for $$\k\$$, as it contradicts the previous assertion, but it results in a shorter regex)
^                    # tail = K
# Divide \3 = K, K/2, K/5, or K/10
(                    # \1 = tail == K
(                # \2 = \3 or \3*5
(x+)         # \3 = any positive number satisfying the assertions below
(\3{4})?     # optionally add \3*4 to \2
)                # tail -= \2
\2?              # optionally, tail -= \2
)
,                    # Assert tail == 0;
# tail = N
(?!\1+$) # Assert N is not divisible by K \3+$                 # Assert N is divisible by \3


# Regex (Ruby), 31 bytes

^(((x+)\3{4}?)\2?),(?!\1+$)\3+$


Try it online!

This is a straight port of the above ECMAScript regex. Ruby allows multiple back-to-back quantifiers in situations where the meaning is unambiguous, so (\3{4})? can be done as \3{4}? instead. In other regex engines, the ? in \3{4}? would be interpreted as modifying the quantifier to be lazy (non-greedy), even though on a constant quantifier that has no effect.

Note that (A{N,M})? cannot be changed to A{N,M}? in Ruby, because in that case the ? does act as a lazy modifier to the quantifier.

### Regex (ECMAScript), 171 151 bytes

Just for kicks, let's see if we can port the algorithm from xnor's Python answer and many subsequent answers, which is to assert that $$\n≢0\pmod k\ \land\ 10n≡0\pmod k\$$. The challenge here is that we can't directly compute $$\10n\$$, since regex can't operate on numbers larger than any of the inputs (of course, we could if $$\k\ge 10n\$$, but in most cases it won't). So we need to emulate the calculation of $$\10n\$$ modulo $$\k\$$.

^(?=(x*),\1*(x*))(?=(?=\2\B(x*))(x*(?=\3\3)|\2\2))(?=((?=\4(x*))x*(?=\3\6)|\4\2))(?=((?=\5(x*))x*(?=\3\8)|\5\2))(?=((?=\7(x*))x*(?=\3\10)|\7\2))\9{2}\b

Try it online!

^                      # tail = K
(?=(x*),\1*(x*))       # \1 = K; \2 = N % K
(?=
(?=
\2             # tail -= \2
\B             # Assert 0 < \2 < K
(x*)           # \3 = tail == K - \2 == also a tool to make tail = \2
)
(                  # \4 = (\2 + \2) % K == (N * 2) % K
x*(?=\3\3)     # \4 = head = \2 - \3
|                  # if above failed due to K - \2 > \2 then fall back on:
\2\2           # \4 = \2 + \2
)
)
(?=((?=\4(x*))x*(?=\3\6 )|\4\2))   # \5 = (\4 + \2) % K == (N * 3) % K
(?=((?=\5(x*))x*(?=\3\8 )|\5\2))   # \7 = (\5 + \2) % K == (N * 4) % K
(?=((?=\7(x*))x*(?=\3\10)|\7\2))   # \9 = (\7 + \2) % K == (N * 5) % K

\9{2}                  # tail = tail - \9*2 == K - \9*2
\b                     # Assert tail==K or tail==0, which is equivalent to
# asserting (N * 5 * 2) % K == 0


This could be shortened greatly in regex flavors with forward-declared backreferences, but it'd still be much longer than 33 bytes.

# Retina 0.8.2, 42 40 bytes

\d+
$* ^(?=(.+)\1*,\1+$)(\1\B|\1{5})\2?,


Try it online!

• I felt sure that there was a golf of that 2, 5, 10 divisibility check, and sure enough you found one.
– Neil
Jul 3, 2022 at 7:09
• @Neil I figured you might have been thinking that :-) At the time I posted it though, I thought I'd exhausted the possibilities. Jul 3, 2022 at 7:12

# 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.

# Java, 35 bytes

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


Try it online!

# Java, 20 bytes

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


n=>k=>/\..$/.test(n/k)  Checks that there is exactly one decimal place after division. Try it online! # 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. # 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  ## Kustom, 30 bytes this one is really small for a language for making android widgets O.o $tc(cut,gv(n)/gv(k),-2,1)=.\$