Euclid's algorithm for GCF two numbers GCF(a, b)=GCF(b, a mod b). I saw this implemented in Python as follows:
GCF(a, b)=GCF(b, a mod b)
def gcf(a, b): return b and gcf(b, a%b) or a
I don’t understand how to parse this function or specifically how logical logic is applied to integers. Eg gcf(42, 56) = 14. When I go through it, I see that ultimately the recursive part returns zero. I follow 0 or n == nand 0 and n == 0. However, when I compare a pair of nonzero numbers with logic and / or logic, I do not understand what is happening and why.
gcf(42, 56) = 14
0 or n == n
0 and n == 0
Can anyone get through this feature?
Python 'and' ' . , .
0 n - n
0 n - 0
a and b or c - (a ? b : c) C.
a and b or c
(a ? b : c)
4.6 Python python .
:
gcf (42, 56)gcf (56, 42)//, b , 42% 56 (= 42) gcf (42, 14)// b , 56% 42 (= 14) gcf (14, 0)//, b , 42% 14 (= 0) return a//, b , (14)
. python, , , popping , 1/true.
, / , , .
, , , .
x or y → x, if x: , y.
x or y
x
if x:
y
x and y → x, if x: , y.
x and y
, GvR . , , , , , , x if C else y .
x if C else y
, ... . REPL :)
b , gcf (b, a% b) (recurse). b , .
Source: https://habr.com/ru/post/1781076/More articles:The fastest way to load sketch pixel values in Java - javadisplaying texture on a square (Android) - androidHow to do this if before multiple inserts in SQL Server - sqlVim NerdCommenter: добавление нового типа файлов в vimrc - vimHiding the caption attribute over the mouse - attributesHow does jslint work? - javascriptWhat are some differences between apps and applets? - javaHow to play YouTube videos in Silverlight? - youtubeBold and italics - phpCreate a blog network with WordPress 3 - wordpressAll Articles