how to sum digits of a number

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

how to sum digits of a number

Post by mattmaier » Thu Jun 19, 2014 6:59 pm

I want to implement a pattern where the output is the input plus the sum of the individual digits of the input.

So, if the input was 839 the output would be 839 + (8 + 3 + 9) = 859.

Is there a built-in function that will allow me to break the number up into its individual digits?

If not, I was thinking I could divide by 10, then cut off the remainder and put it into an array; then repeat as long as the number is greater than 1. Something like that.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: how to sum digits of a number

Post by magice » Thu Jun 19, 2014 7:18 pm

How about something like this?

Code: Select all

 repeat with i = 1 to the number of characters in tNumber
      put character i of tNum & comma after tResult
  end repeat

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: how to sum digits of a number

Post by sefrojones » Thu Jun 19, 2014 7:36 pm

I'm not sure if this is the best way to do what you're asking, but check this out:

1. Open a new stack

2. Drag one field and one button onto your stack.

3. Put this script in your button:

Code: Select all

local tEquation,tInput

on mouseUp
   //If there are no numbers in the input fld then exit mouseup handler
   if fld 1 is empty then exit mouseup
   //clear and set up variables
   Put empty into tEquation
   Put fld 1 into tInput
   //build equation
   Put tInput &&"+ (" after tEquation
   repeat with x= 1 to the number of chars in tInput
      put char x of tInput & "+" after tEquation
   end repeat
   //clean up Equation
   delete the last char of tEquation
   put ")" after tEquation
   //Solve
   answer tEquation &&"="&& the value of tEquation
    end mouseUp
4. Put the numbers into the fld. (e.g. 839)

5. Press the button.

6. Read the script. Can you see how it's working?


--Sefro

mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

Re: how to sum digits of a number

Post by mattmaier » Thu Jun 19, 2014 10:55 pm

This seems to be working. The important bit that I was asking about is:

Code: Select all

repeat for each character tChar in temp
         put theDigits & (tChar & comma) into theDigits
      end repeat
This is the whole thing so far. Now I get to work on the second pattern. The goal is to output a list of any numbers not visited by either of these two patterns.

Code: Select all

on mouseUp
   //variables
   put field "fieldStartValue" into tStart
   put field "fieldEndValue" into tEnd
   
   //clear display field
   put empty into field "fieldOutput"
   
   //notVisited: create array with every round number from start to end
   put tStart into temp
   repeat while (temp <= tEnd)
      put temp into notVisited[temp]
      put (temp+1) into temp
   end repeat
   
   //FIRST PATTERN: n+(sum of digits)
   put tStart into temp
   repeat while (temp <= tEnd)
      //! remove temp from array "notVisited"
      //all of the elements are named and filled with the same thing
      delete variable notVisited[temp]
      //convert temp into list of each digit; theDigits
      put empty into theDigits
      repeat for each character tChar in temp
         put theDigits & (tChar & comma) into theDigits
      end repeat
      //add up all of the digits & the original seed value
      put sum(theDigits)into tResult
      put tResult + temp into temp
      //now temp should have the next number in the pattern
   end repeat //return to the beginning of the loop
   
   /*
   //SECOND PATTERN: if n is even, then n/2, else 3n+1
   put tStart into temp
   repeat while (temp <= tEnd)
      delete variable notVisited[temp]
      if (temp mod 2 is 0)
      then
         put temp/2 into temp
      else
         put (temp * 3) + 1 into temp
      end if
      //now temp should have the next number in the pattern
   end repeat //return to the beginning of the loop
   */
   
   //DISPLAY RESULTS
   combine notVisited using return
   put notVisited into field "fieldOutput"
end mouseUp

Post Reply