Talk:Hailstone sequence: Difference between revisions

Line 4:
 
:: :-)<br>--[[User:Paddy3118|Paddy3118]] 04:17, 25 June 2010 (UTC)
 
==Fortran==
The following fortran code should be correct (at least to display the sequence) but it doesn't seem to work. Most numbers will be processed correctly but certain numbers will cause a weird arithmetic error during which the program will multiply by 3 and SUBTRACT 1 instead of adding, resulting in an infinite loop. The smallest of these numbers appears to be 113383, might this be a specific problem with my machine or compiler? Nothing too special about that number other than that it is prime, more of these glitched numbers seem to appear as the numbers get higher, almost all numbers above 800,000,000 will result in infinite loop.
 
PROGRAM Hailstone
IMPLICIT NONE
INTEGER :: num, tnum
outer: DO
PRINT *, "Type in your number (0 terminates)"
READ *, num
IF (num .LE. 0) EXIT
inner: DO
tnum = num/2
IF (2*tnum .EQ. num) THEN ! num is even
num = tnum
ELSE ! num is odd
num = 3*num+1
END IF
PRINT *, num
IF (num == 1) THEN
EXIT
END IF
END DO inner
END DO outer
END PROGRAM Hailstone
Anonymous user