45 lines
1.1 KiB
Plaintext
45 lines
1.1 KiB
Plaintext
/* NetRexx */
|
|
|
|
options replace format comments java crossref savelog symbols binary
|
|
|
|
do
|
|
start = 27
|
|
hs = hailstone(start)
|
|
hsCount = hs.words
|
|
say 'The number' start 'has a hailstone sequence comprising' hsCount 'elements'
|
|
say ' its first four elements are:' hs.subword(1, 4)
|
|
say ' and last four elements are:' hs.subword(hsCount - 3)
|
|
|
|
hsMax = 0
|
|
hsCountMax = 0
|
|
llimit = 100000
|
|
loop x_ = 1 to llimit - 1
|
|
hs = hailstone(x_)
|
|
hsCount = hs.words
|
|
if hsCount > hsCountMax then do
|
|
hsMax = x_
|
|
hsCountMax = hsCount
|
|
end
|
|
end x_
|
|
|
|
say 'The number' hsMax 'has the longest hailstone sequence in the range 1 to' llimit - 1 'with a sequence length of' hsCountMax
|
|
catch ex = Exception
|
|
ex.printStackTrace
|
|
end
|
|
|
|
return
|
|
|
|
method hailstone(hn = long) public static returns Rexx signals IllegalArgumentException
|
|
|
|
hs = Rexx('')
|
|
if hn <= 0 then signal IllegalArgumentException('Invalid start point. Must be a positive integer greater than 0')
|
|
|
|
loop label n_ while hn > 1
|
|
hs = hs' 'hn
|
|
if hn // 2 \= 0 then hn = hn * 3 + 1
|
|
else hn = hn % 2
|
|
end n_
|
|
hs = hs' 'hn
|
|
|
|
return hs.strip
|