1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

How to add the digits of a number until there is one digit left.

Discussion in 'Tutorials' started by xyden.konos, Mar 31, 2017.

Tags:
  1. xyden.konos

    xyden.konos Guest

    GM Version: GM 1.4
    Target Platform: All
    Download: N/A
    Links: N/A

    Summary:
    For my project, I needed to get a number and add all the digits together until I was left with a single digit between 1 and 9, but as I searched for a way to do that, I couldn't find any gm blog post or tutorial on how to go about doing it. I had to translate a javascript tutorial to gml to accomplish what I needed, and this is what I came up with. Of course it's a first draft, and I plan on editing it in the future in order to optimize it further (if possible), but it works. Though it only works with nonzero positive integers.

    Tutorial:

    your_number = //This is the number whose digits you want to add;

    var r_len = string_length(string(your_number));
    /*gml doesn't have a function to return the amount of digits a real has, so to work around that: first) the real is converted to a string and second) using string_length, it returns the length of the string and saves it to a variable; essentially it gives you the amount of digits your_number has.*/​

    while(r_len > 1) {
    /*this while loop checks the variable holding the amount of digits of the input, and if it bigger than 1, it runs the for loop again which is what does the calculations.*/​

    var sum = 0; //initialized to zero in order to add reals to this variable.​

    for(var i = 0; i < r_len; i++) { //runs the loop until i equals r_len.​

    sum += your_number mod 10; /*this takes the last digit in the input and adds it to the variable.*/
    your_number = floor(your_number / 10); /*and this snips off the last digit of the input since it isn't needed.*/​

    } /*the loop works by adding the last digit to the sum variable then reducing the input by one digit and continues until all digits have been worked through, and all have been added to the sum.*/​

    your_number = sum;
    r_len = string_length(string(your_number));
    //these two lines set up r_len to be reevaluated by the while loop.​

    }

    //once the loop breaks, the your_number variable will be the end result.

    your_number = //This is the number whose digits you want to add;

    var r_len = string_length(string(your_number));

    while(r_len > 1) {

    var sum = 0;

    for(var i = 0; i < r_len; i++) {​

    sum += your_number mod 10;
    your_number = floor(your_number / 10);​

    }​

    your_number = sum;
    r_len = string_length(string(your_number));​

    }

    If you have any questions or suggestions, leave them in the comments. I'll reply as soon as I can.
     
    Last edited by a moderator: Apr 3, 2017
  2. JRLS

    JRLS Member

    Joined:
    Jan 20, 2017
    Posts:
    89
    If your_number is negative (e.g. -13), your code will loop forever.
    also, if your_number = 0, then it wont give you a number between 1 and 9.
    I'm not sure if that's on purpose?
     
    Last edited: Apr 2, 2017
    chance likes this.
  3. xyden.konos

    xyden.konos Guest

    The negative number is an oversight on my part, so haha whoops. Though a simple abs() function can solve that. As for the result being zero, it's a minor thing. I'm using this code to take the amount of key board presses the player has done and get a number between 1 and 9 (if it's zero, it's not a big deal). Then I'm using that number to slightly tweak the stats of an enemy.
     
  4. chance

    chance predictably random Forum Staff Moderator

    Joined:
    Apr 22, 2016
    Posts:
    798
    Perhaps not a big deal for your own applications, but @JRLS observation is valid. And you can't assume that everyone will use your script for the same purposes as you. So you should edit your post comments explaining that it's limited to non-zero positive numbers. Or alternatively, modify the script so it's more general.
     
  5. JRLS

    JRLS Member

    Joined:
    Jan 20, 2017
    Posts:
    89
    Maybe an idea to make a function (script) out of it?
    (out of the top of my head, so might contains errors)

    Code:
    function scr_onedigit(argument0) : integer
    {
      // return one digit between 0 and 9
      var your_number = abs(argument0)
      var r_len = string_length(string(your_number));
    
      while(r_len > 1)
      {
         var sum = 0;
         for(var i = 0; i < r_len; i++)
         {
             sum += your_number mod 10;
             your_number = floor(your_number / 10);
         }
         your_number = sum;
         r_len = string_length(string(your_number));
      }
      return your_number ;
    }
     
  6. xyden.konos

    xyden.konos Guest

    I realize this script is specialized. The idea behind the code is specialized since adding digits of a number will always result in a number between 1 and 9 unless the input is negative or zero. I'll put in some thought to generalize my code though, because the reason I decided to post this is to help others, but if it only helps me then it isn't much help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice