Created
June 27, 2022 10:38
-
-
Save renancaraujo/0511030a028932d8645ccd0202387064 to your computer and use it in GitHub Desktop.
GCD dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
print(gcd([144, 96, -24, -1200, 671])); | |
} | |
int gcd(List<int> els) { | |
final minF = els.reduce((a, b) { | |
assert(b != 0, "element cannot be zero"); | |
return min(a, b.abs()); | |
}); | |
for(int i = minF; i >= 2; i--) { | |
if(els.every((el) => el % i == 0)) { | |
return i; | |
} | |
} | |
return 1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment