| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | SAKO | 250326T173257Z | Acrimori |
| 039 | Forth gforth | 240909T175422Z | reffu |
| 043 | Setanta | 240908T173429Z | bb94 |
| 004 | Nekomata | 240114T022254Z | alephalp |
| 025 | Ruby | 240907T020215Z | Jordan |
| 077 | TSQL | 240208T161331Z | Sam CD |
| 070 | Scratch | 240115T084914Z | Rhaixer |
| 097 | TypeScript's type system | 240117T025722Z | noodle p |
| 018 | APLDyalog Unicode | 240117T022855Z | Youserna |
| 037 | TIBASIC | 240117T002324Z | Youserna |
| 008 | Jelly | 240114T152948Z | Nick Ken |
| 037 | Perl 5 Minteger ap | 240116T212739Z | Xcali |
| 051 | Funge98 | 240116T055129Z | Alt Shif |
| 009 | x8632 machine code | 240115T225549Z | Peter Co |
| 006 | 05AB1E | 240115T143217Z | Kevin Cr |
| 023 | Charcoal | 240114T154844Z | Neil |
| 010 | Pyth | 240114T142651Z | Mukundan |
| 013 | Uiua SBCS | 240114T013404Z | chunes |
| 027 | C gcc | 240114T121223Z | G. Sliep |
| 033 | R | 240114T115404Z | Nick Ken |
| 035 | Retina 0.8.2 | 240114T112856Z | Neil |
| 074 | Google Sheets | 240114T075000Z | z.. |
| 047 | Go | 240114T051505Z | bigyihsu |
| 035 | Python 2 | 240114T021158Z | enzo |
| 027 | JavaScript ES6 | 240114T012421Z | Arnauld |
SAKO, 91 bytes
PODPROGRAM:D(N,M)
CALKOWITE:N,M
1)A=MOD(N,M)
N=DIV(N,M)
M=A
GDYM>1:1,INACZEJ2
2)LINIIM
WROC
Prints a CR for 0 and LF for 1.
Full programme version, 92 bytes
CALKOWITE:N,M
CZYTAJ:N,M
1)A=MOD(N,M)
N=DIV(N,M)
M=A
GDYM>1:1,INACZEJ2
2)LINIIM
STOP2
KONIEC
Forth (gforth), 39 bytes
: f dup 1 > if /mod swap recurse then ;
Explanation
Pretty straightforward in forth. Recursive solution is fewer bytes because the non-recursive solution needs a loop in the form of begin while repeat, which adds an extra word.
Code Explanation
: f \ begin word definition
dup 1 > \ duplicate remainder/divisor and check if it's greater than 1
if \ if it is, begin conditional code
/mod \ get the quotient and remainder from dividing the two numbers
swap \ reverse the stack elements because /mod uses the opposite order
recurse \ call self recursively
then \ end conditional code
; \ end word definition
Setanta, 43 bytes
gniomh f(a,b){ma b>1 b=f(a//b,a%b)toradh b}
Non-recursive solution, 51 bytes
gniomh(a,b){nuair-a b>1{c:=a//b b=a%b a=c}toradh b}
Nekomata, 4 bytes
ʷ{Ƶþ
ʷ{Ƶþ
ʷ{ While; run the following code until it fails
Ƶ Check if the absolute value of the top of the stack is greater than 1
þ Divmod
Both Ƶ and þ are new built-ins in Nekomata 0.5.1.
T-SQL 77 bytes
declare @x int = 88, @y int = 7, @z int
while @y > 1 BEGIN
select @z = @x/@y, @y = @x-(@z*@y)
set @x = @z
end
select @y
Scratch, 77 70 bytes
Ports @enzo's answer. Run without screen refresh.
define(x)(y
if<(y)>(1)>then
([floor v]of((x)/(y)))((x)mod(y
else
say(y
TypeScript's type system, 97 bytes
//@ts-ignore
type M<A,B,T=[]>=A extends[...B,...infer I]?M<I,B,[...T,1]>:A extends[1]|[]?A:M<T,A>
Try it at the TypeScript playground!
I/O is unary numbers / tuples of 1s.
Modified from my solution to Division and remainder.
APL(Dyalog Unicode), 18 bytes SBCS
{1<⍵:(⌊⍺÷⍵)∇⍵|⍺⋄⍵}
A dfn that takes the first number as the left argument and the second number as the right argument.
TI-BASIC, 37 bytes
While 1<Ans(2
Ans→L
Ans(1)/Ans(2
int({Ans,ʟL(2)fPart(Ans
End
Ans(2
Takes input in Ans. Replace the second to fourth lines with int({Ans(1)/Ans(2),remainder(Ans(1),Ans(2 for -2 bytes if remainder( is supported.
Jelly, 8 bytes
d/ỊṪ¬Ɗ¿Ṫ
A monadic link taking a pair of non-negative integers and returning 1 or 0.
Explanation
Ɗ¿ | While the following is true:
Ị | - <= 1
Ṫ | - Tail (i.e. second list member)
¬ | - Not
d/ | … Reduce using divmod
Ṫ | Tail
If we were allowed to guarantee the second input were non-zero, this is shorter:
Jelly, 7 bytes
d/%/¿ṪỊ
x86-32 machine code, 9 bytes
x86-64 machine code, 10 bytes, same asm source
Args in EAX (dividend), ECX (divisor), return value in ECX (0 or 1 final remainder).
To call from C, gcc -mregparm=3 will put args in EAX, EDX, ECX in that order (so a dummy second arg works), but won't look for retval in ECX. Returning in EAX costs 1 extra byte for xchg eax,ecx at the end.
Args must be signed non-negative. (At least the dividend must be; the divisor can be full-range 32-bit unsigned. And the first remainder must also be non-negative). Divisor must be non-zero or the first iteration will fault.
divmod_repeat:
.loop:
99 cdq ; zero-extend (for numbers that fit as non-negative integers)
F7F1 div ecx ; EAX = EDX:EAX/ecx unsigned; EDX = remainder
89D1 mov ecx, edx
4A dec edx ; 1 byte instruction in 32-bit mode
7FF8 jg .loop ; }while(remainder-- > 1); or }while(--rem > 0)
C3 ret
; cmp ecx, 2 ; simple but less golfed version
; jae .loop ; }while(new_divisor >= 2);
This shows a hexdump of the 32-bit machine code on the left, from a NASM listing. The x86-64 version (still using 32-bit operand-size) uses FFCA dec edx.
Not much scope for golfing, other than choosing a good calling convention. div only works with EDX:EAX / src, producing its outputs in EDX (remainder) and EAX (quotient). We don't want to touch quotient, so all the special-case accumulator short-form encodings of instructions like xchg eax, reg and cmp al, imm8.
But we can destroy the old copy of the remainder in EDX after copying it to ECX as the new divisor (or return value). So we can use 1-byte (in 32-bit mode) dec reg to set FLAGS instead of cmp. The next iteration's cdq will overwrite EDX anyway, and we need cdq anyway.
We can only use signed conditions with dec because it leaves CF unmodified, but is otherwise like sub or cmp ecx, 1.
05AB1E, 6 bytes
[`D!#‰
Try it online or verify all test cases.
Explanation:
[ # Start an infinite loop:
` # Pop and push both values in the pair separately to the stack
# (which will use the implicit input-pair in the first iteration)
D # Duplicate the top value (the remainder)
! # Take its factorial (0 becomes 1; 1 remains 1; ≥2 becomes larger)
# # If it's truthy (==1), stop the infinite loop
# (after which the duplicated remainder is output implicitly as result)
‰ # (else) Divmod the two values
Charcoal, 23 bytes
NθNηW∧⊖ηη«≔﹪θιη≔÷θιθ»Iη
Try it online! Link is to verbose version of code. Explanation: Lack of divmod and recursion makes this annoyingly long.
NθNη
Input the two values.
W∧⊖ηη«
Compare the second value against both 1 and 0, but in such a way that if it is neither than the implicit loop variable has the same value.
≔﹪θιη≔÷θιθ
Modulo and divide the first value by the copy of the second, thus allowing the second and first values to be safely overwritten.
»Iη
Output the final value.
Uiua SBCS, 13 bytes
⊙◌⍢(:⌊⊃÷◿|>1)
⊙◌⍢(:⌊⊃÷◿|>1)
⍢( | ) # while
>1 # top of stack is greater than one
⊃÷◿ # quotient and remainder
⌊ # take the floor of the quotient
: # swap so remainder is on top
⊙◌ # drop the quotient from the stack
R, 33 bytes
f=\(x,y)`if`(y>1,f(x%/%y,x%%y),y)
A recursive function taking two arguments and returning 1 or 0.
Retina 0.8.2, 35 bytes
.+
$*
+`(..+)¶(\1)*(.*)
$3¶$#2$*
^.
Try it online! Takes the integers in reverse order on separate lines but link is to test suite that takes input in the format given in the question. Explanation:
.+
$*
Convert to unary.
(..+)¶(\1)*(.*)
$3¶$#2$*
If the denominator is at least 2, divmod the numerator by the denominator, replacing the denominator by the remainder and the numerator by the quotient.
+`
Repeat until the denominator is reduced to less than 2.
^.
Convert the denominator to 0 or 1.
Google Sheets, 74 bytes
=LET(F,LAMBDA(F,d,m,LET(x,MOD(d,m),IF(x<2,x,F(F,INT(d/m),x)))),F(F,A1,A2))
Simple recursive function that stops the execution when the remainder is 0 or 1.
