PowerShell v2+, 48 bytes
param($x,$y)(1..$x|?{!(("$_*"*$y+1|iex)%$x)})[0]
Takes input $x
and $y
. Constructs a range from 1
to $x
, then uses Where-Object
to filter those numbers. The filter takes the string "$_*"
(i.e., the current number with an asterisk) and uses string-multiplication to concatenate those $y
times, then tacks on a final 1
at the end, then pipes that to iex
(short for Invoke-Expression
and similar to eval
). This takes the place of [math]::Pow($_,$y)
, since PowerShell doesn't have an exponentiation operator, and is two bytes shorter. That's fed into the modulo operator %
with $x
-- thus, if it's divisible, this will be 0
, so we encapsulate that in parens and take the Boolean-not !(...)
thereof. Thus, if its divisible, it'll be included by this filter, and all other numbers will be excluded.
Finally, we encapsulate the resultant numbers in parens (...)
and take the [0]
index. Since the range entered sorted 1..$x
, this will be the smallest. That's left on the pipeline and printing is implicit.
Test cases
PS C:\Tools\Scripts\golfing> (26,2),(96,2),(32,3),(64,9),(27,3)|%{($_-join', ')+' -> '+(.\smallest-positive-number-divisor.ps1 $_[0] $_[1])}
26, 2 -> 26
96, 2 -> 24
32, 3 -> 4
64, 9 -> 2
27, 3 -> 3
X
always be greater thanY
? \$\endgroup\$X
is less thanY
, and it can reduce the length of some answers (at least mine) ifX
is always greater thanY
. I would rather have thatX
can be either bigger or smaller, but then one test case for the latter would be great. \$\endgroup\$