Calculation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Calculation

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Calculation

    [Originally posted by Vicky]

    Hi... Can anyone help me....

    The program that I'm writing now is simulating the Collatz Problem and determine the number of steps taken to reach 1 for any given intger.The process is that if the number is even, divide that number by 2; If the number is odd, multiply that number by 3 and then add 1. the calculation goes on and on until the number is 1

    I got this problem:
    If the input number is odd then multiply the number by 3 then add 1 to it.

    I call the number intNu..
    I've type intNu= (intNu*3)+1)
    but when i run the program, a message pop up and says i got this overflow problem....
    What should i do to make it work....
    Thankyou

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Calculation

    [Originally posted by Eli Martin]

    Is intNu really overranging? I'm sure you know, but the value range for and int is -32767 thru 32768. If it is truely in range, e-mail me the code and I will take a deeper look at it.

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Calculation

    [Originally posted by JPicasso]

    integer variables can only hold values upto
    32,000 someting.

    try declaring variables as Longs.


  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Calculation

    [Originally posted by Jonathan Barnes]

    On a purely mathematical note, there is one part of the formula I do not quite follow: if the goal is to get to 1 by iteration, I would think it would be something like:

    dim lngnum as long

    do while lngnum <> 1

    if lngnum MOD 2 = 0 then
    lngnum = lngnum / 2
    else
    lngnum = lngnum - 1
    'instead of lngnum * 3 + 1
    endif

    loop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center