Hailstone sequence: Difference between revisions
m
→{{header|Dart}}: Brought Dart code up to date
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
m (→{{header|Dart}}: Brought Dart code up to date) |
||
Line 3,038:
=={{header|Dart}}==
<syntaxhighlight lang="dart">
import 'package:collection/collection.dart';
if(n<=0) {▼
import 'dart:collection';
throw new IllegalArgumentException("start value must be >=1)");▼
List<int> hailstone(int n) {
▲ if(n <= 0) {
}
seq.add(n);
while(n != 1) {
n = n%2 == 0 ?
seq.add(n);
}
return
}▼
}
main() {
for(int i = 1; i <= 10; i++) {
print("h($i) =
}
print("first 4 elements of h(27):
print("last 4 elements of h(27):
print("length of sequence h(27):
int seq = 0, max = 0;
for(int i = 1; i <= 100000; i++) {
if(h.length > max) {
max = h.length;
seq = i;
}
}
print("up to 100000 the sequence h($seq) has the largest length ($max)");
▲}
{{out}}
<pre>h(1) = [1]
h(2) = [2, 1]
h(3) = [3, 10, 5, 16, 8, 4, 2, 1]
h(4) = [4, 2, 1]
h(5) = [5, 16, 8, 4, 2, 1]
h(6) = [6, 3, 10, 5, 16, 8, 4, 2, 1]
h(7) = [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
h(8) = [8, 4, 2, 1]
h(9) = [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
h(10) = [10, 5, 16, 8, 4, 2, 1]
first 4 elements of h(27): [27, 82, 41, 124]
last 4 elements of h(27): [8, 4, 2, 1]
length of sequence h(27): 112
up to 100000 the sequence h(77031) has the largest length (351)</pre>
|