Talk:Hailstone sequence

From Rosetta Code
Revision as of 14:40, 6 June 2011 by 95.97.39.162 (talk) (→‎Euphoria missing: new section)

Brainf***

I have restrained myself from marking the Brainf*** example incorrect as it barely scrapes through at showing the sequence, let alone the other parts to the task. Given the nature of the language I might just sit back and wonder at the masochism. (No doubt in some future war with The Aliens, BF programmers will come into their own by decoding how their spaceships work and programming them to self destruct). --Paddy3118 08:27, 23 June 2010 (UTC)

And if BFers can't do it, the SNUSPers can! --IanOsgood 00:18, 25 June 2010 (UTC)
:-)
--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(im using force 2.0 atm, ancient, I know)? Nothing too special about that number other than that it is prime, more of these glitched numbers seem to appear more frequently as the numbers get higher.


      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
Might it be integer overflow? --Paddy3118 19:44, 13 July 2010 (UTC)
I doubt it, since 113383 is not a power of 2 and the next few integers work fine all the way up to 134379, which is the next number that does this.
It seems to have something to do with the DO-loop since if i remove the loop (essentially making a program that does only one iteration of the hailstone sequence) the arithmetic works fine.
I meant maybe the sequence starting from that number overflowed. Try printing all the variables each time through the loop, or changing the exit condition to less than or equal to 1? --Paddy3118 04:52, 14 July 2010 (UTC)
I'm not sure what you mean but the exit condition isn't working anyway, I've actually seen numbers result a 1,2,1,2,1,2 oscillation, ignoring the fact that 1 was reached already.
Definitely an integer overflow. Not at the number 113383, but at the number 827370449 which is reached at the 119th step in the sequence starting at 113383. By attempting to compute 3n+1 from there, the result reached is 2482111348 upon which the variable num overflows to -1812855948. You can postpone this failure by defining your variables INTEGER*8 (I'm also sure you never saw a 1,2,1,2... but a -1,-2,-1,-2; which you can convince yourself is perfectly legal if you can ever reach negative numbers.) Sgeier 20:39, 24 August 2010 (UTC)

Euphoria missing

Not a big one, but it's a pity that Euphoria is missing --95.97.39.162 14:40, 6 June 2011 (UTC)