13 lines
304 B
Dart
13 lines
304 B
Dart
|
class CollatzConjecture {
|
||
|
int steps(int start) {
|
||
|
if (start < 1)
|
||
|
throw new ArgumentError('Only positive integers are allowed');
|
||
|
if (start == 1) return 0;
|
||
|
if (start.isEven) {
|
||
|
return 1 + this.steps(start ~/= 2);
|
||
|
} else {
|
||
|
return 1 + this.steps(start * 3 + 1);
|
||
|
}
|
||
|
}
|
||
|
}
|