/    Sign up×
Community /Pin to ProfileBookmark

Hailstone Sequence Counting Code Help!

I’m making a code to execute a Hailstone Sequence and need some help because I’m not sure how to really code it for this sequence, here are the steps for the sequence.

  • 1. Start with any positive integer.

  • 2. If that number is odd, then multiply it by three and add one; otherwise, divide it by two.
  • here is my coding so far:

    [CODE]<html>
    <head>
    <title> Hailstone Sequence </title>
    <script type=”text/javascript”>
    function Countdown()
    // Assumes: document.CountForm.count contains a number
    // Results: displays countdown from that number in document.CountForm.Output
    {
    var count;

    count = parseFloat(document.CountForm.count.value); // GET INITIAL VALUE
    document.CountForm.Output.value = “”; // CLEAR TEXT AREA

    while (count > 0) { // WHILE NOT AT 0,
    document.CountForm.Output.value = // DISPLAY COUNT
    document.CountForm.Output.value + count + “n”;
    count = count – 1; // AND DECREMENT
    }

    document.CountForm.Output.value =
    document.CountForm.Output.value + “STUCK!”;
    }
    </script>
    </head>

    <body>
    <div style=”text-align:center”>
    <form name=”CountForm”>

    Start of the sequence: <input type=”text” name=”count” size=4 value=10 />
    <br /><br />
    <input type=”button” value=”Begin Hailstone Sequence” onClick=”Countdown();” />
    <br /><br />
    <textarea name=”Output” rows=15 cols=40 wrap=”virtual”></textarea>
    </form>
    </div>
    </body>

    </html>[/CODE]

    to post a comment
    JavaScript

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @heyushooshauthorDec 14.2007 — I've worked a little on it to make it a little better but it still doesn't work. Here's what I've got so far:

    [CODE]<html>
    <head>
    <title> Hailstone Sequence </title>
    <script type="text/javascript">

    function Countdown()
    // Assumes: document.CountForm.count contains a number
    // Results: displays countdown from that number in document.CountForm.Output
    {
    var count;

    count = parseFloat(document.CountForm.count.value); // GET INITIAL VALUE
    document.CountForm.Output.value = ""; // CLEAR TEXT AREA


    if (count%2 == 0)
    document.CountForm.Output.value =
    document.CountForm.Output.value + count + "n";
    count = count*3+1

    else (count%2 =/= 0)
    document.CountForm.Output.value =
    document.CountForm.Output.value + count + "n";
    count = (count/2)

    while (count > 0) { // WHILE NOT AT 0,
    document.CountForm.Output.value = // DISPLAY COUNT
    document.CountForm.Output.value + count + "n";
    count = count - 1; // AND DECREMENT
    }

    document.CountForm.Output.value =
    document.CountForm.Output.value + "STUCK!";
    }
    </script>
    </head>

    <body>
    <div style="text-align:center">
    <form name="CountForm">

    Start of the sequence: <input type="text" name="count" size=4 value=10 />
    <br /><br />
    <input type="button" value="Begin Hailstone Sequence" onClick="Countdown();" />
    <br /><br />
    <textarea name="Output" rows=15 cols=40 wrap="virtual"></textarea>
    </form>
    </div>
    </body>
    </html>
    [/CODE]
    ×

    Success!

    Help @heyushoosh spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...