misc/vim: inform the user when no results are found
[nit.git] / examples / rosettacode / hailstone.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 # This program is public domain
3 # Copyright 2014 amineorion <amineorion@gmail.com>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17 # Task: Hailstone sequence
18 # SEE: <http://rosettacode.org/wiki/Hailstone_sequence>
19 #
20 # Not optimize version.
21
22 module hailstone
23
24 fun hailstone (n: Int): Array[Int]
25 do
26 var sequence = [n]
27 while n != 1 do
28 if n.bin_and(0x01) == 0 then
29 n = n / 2
30 else
31 n = 3 * n + 1
32 end
33 sequence.add(n)
34 end
35 return sequence
36 end
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]}"
43
44 var max = hailstone(1).length
45 var max_sequence = hailstone(1)
46 var max_element = 1
47
48 for i in [2..100000] do
49 var sequence = hailstone(i)
50 if max < sequence.length then
51 max = sequence.length
52 max_element = i
53 max_sequence = sequence
54 end
55 end
56 print "The number with longest sequence is {max_element} " +
57 "with length of {max_sequence.length}"