X
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:
If you have any questions or suggestions, leave them in the comments. I'll reply as soon as I can.
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));
while(r_len > 1) {
}
//once the loop breaks, the your_number variable will be the end result.
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.*/
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.
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 r_len = string_length(string(your_number));
while(r_len > 1) {
var sum = 0;
for(var i = 0; i < r_len; i++) {
for(var i = 0; i < r_len; i++) {
sum += your_number mod 10;
your_number = floor(your_number / 10);
your_number = floor(your_number / 10);
}
your_number = sum;
r_len = string_length(string(your_number));
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: