Skip to content

Instantly share code, notes, and snippets.

@renancaraujo
Created June 27, 2022 10:38
Show Gist options
  • Save renancaraujo/0511030a028932d8645ccd0202387064 to your computer and use it in GitHub Desktop.
Save renancaraujo/0511030a028932d8645ccd0202387064 to your computer and use it in GitHub Desktop.
GCD dart
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