Talk:Hailstone sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 33: Line 33:
: Might it be integer overflow? --[[User:Paddy3118|Paddy3118]] 19:44, 13 July 2010 (UTC)
: Might it be integer overflow? --[[User:Paddy3118|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.
: 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.

Revision as of 21:42, 13 July 2010

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.