1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 # This program is public domain
3 # Copyright 2014 amineorion <amineorion@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Task: Hailstone sequence
18 # SEE: <http://rosettacode.org/wiki/Hailstone_sequence>
20 # Not optimize version.
24 fun hailstone
(n
: Int): Array[Int]
28 if n
.bin_and
(0x01) == 0 then
37 var sequence27
= hailstone
(27)
38 var size27
= sequence27
.length
39 print
"Sequence for 27 has {size27} begin with: {sequence27[0]}, " +
40 "{sequence27[1]}, {sequence27[2]}, {sequence27[3]} " +
41 "and end with: {sequence27[size27 - 4]}, {sequence27[size27 - 3]}, " +
42 "{sequence27[size27 - 2]}, {sequence27[size27 - 1]}"
44 var max
= hailstone
(1).length
45 var max_sequence
= hailstone
(1)
48 for i
in [2..100000] do
49 var sequence
= hailstone
(i
)
50 if max
< sequence
.length
then
53 max_sequence
= sequence
56 print
"The number with longest sequence is {max_element} " +
57 "with length of {max_sequence.length}"