factor / factor

Factor programming language

Home Page:https://factorcode.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gcd documentation mixes up x and y WRT value of a

CharnelMouse opened this issue · comments

The documentation for gcd ( x y -- a d ) states the following:

Computes the positive greatest common divisor d of x and y, and another value a satisfying: a*y = d mod x

This is incorrect: a is the required multiplier for a*x = d mod y.
Example code to show this:

USE: backtrack
: check-gcd ( x y -- ? ) 2dup gcd [ * ] dip - swap mod 0 = ;
: check-gcd-opp-spec ( x y -- ? ) [ swap ] [ gcd ] 2bi [ * ] dip - swap mod 0 = ;
[ 1 30 [a..b] amb 1 30 [a..b] amb 2dup check-gcd not must-be-true 2array . t ] [ "All good." print ] if-amb drop
[ 1 30 [a..b] amb 1 30 [a..b] amb 2dup check-gcd-opp-spec not must-be-true 2array . t ] [ "All good." print ] if-amb drop

This prints

{ 2 1 }
All good.

Does that mean the only inputs that fails for are 2 1 gcd ?

It's just the first/simplest failing case that backtrack finds.
The second line is for the second search, and shows there are no failing cases when checking for a*x = d mod y instead.
If you exclude cases where an input is equal to 1, the first failing case for a*y = d mod x it finds is { 2 5 }.
Specifically, it returns -2 1 instead of 1 1.