# 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

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


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

# 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

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

# Jelly, 6 bytes

×⁵ọ¹ȧ%


Try it online!

# 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, 41 bytes

\d+
$* A^(1+),\1+$
r1\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.

r1\G
10$*  Multiply only the dividend by 10. ^(1+),\1+$


Ensure divisibility.

# 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


# 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

# 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, 6 bytes

/Ṫt\.=


Try it Online!

Checks if the second last character is "."

# 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


Try it online!

# JavaScript, 22 bytes

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


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

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