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.
how to sum digits of a number
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: how to sum digits of a number
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
-
- Livecode Opensource Backer
- Posts: 447
- Joined: Mon Jan 23, 2012 12:46 pm
Re: how to sum digits of a number
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:
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
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
5. Press the button.
6. Read the script. Can you see how it's working?
--Sefro
Re: how to sum digits of a number
This seems to be working. The important bit that I was asking about is:
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
repeat for each character tChar in temp
put theDigits & (tChar & comma) into theDigits
end repeat
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