Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Greatest Common Factor (Euclidean algorithm, aka Euclid's algorithm)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
def GCF(n1,n2): '''Finds the greatest common factor between two numbers using the Euclidean algorithm, aka Euclid's algorithm''' '''Author:Wazim Karim''' dividend = max(n1,n2) #dividend is bigger of two numbers divisor=min(n1,n2) #divisor is smaller of two numbers remainder=dividend%divisor #calculate the remainder if remainder == 0: return divisor #if divisor goes into dividend with no remainder, GCF is divisor while remainder != 0: #so long as the divisor does not go into dividend without a remainder dividend=divisor #the divisor becomes the dividend divisor=remainder #the remainder becomes the divisor remainder = dividend%divisor #calculate the reminder if remainder == 0: #if divisor goes into dividend with no remainder, GCF is divisor return divisor #so return the divisor #gcf of two numbers: print(GCF(30,15)) #gcf of more than 2 numbers by nesting: print(GCF(GCF(128,GCF(32,12)),2))
[
+
]
Show input
edit mode
|
history
|
discussion