Help with placing iterated function results into a list
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 4
- Joined: Mon Jan 09, 2012 4:47 pm
Help with placing iterated function results into a list
I was trying to write a simple program to factor numbers. I was writing a function for each number (i.e.
On factorThisNumber
divide thisnumber by 2
if thisnumber is an integer then
put thisnumber & "*2" into field Factors2
end if
multiply thisnumber by 2
End factorThisNumber
...then for 3, 4, etc...
This was getting quite tedious, so I thought I would try my hand at creating an iteration. I know that the math and syntax is correct in my iteration because it works great for 1... but I do not know how to make all the factors go into a single list, or to have them go into different fields. Suggestions?
Thanks in advance.
Carrie
On factorThisNumber
divide thisnumber by 2
if thisnumber is an integer then
put thisnumber & "*2" into field Factors2
end if
multiply thisnumber by 2
End factorThisNumber
...then for 3, 4, etc...
This was getting quite tedious, so I thought I would try my hand at creating an iteration. I know that the math and syntax is correct in my iteration because it works great for 1... but I do not know how to make all the factors go into a single list, or to have them go into different fields. Suggestions?
Thanks in advance.
Carrie
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Help with placing iterated function results into a list
Hi,
Do you mean find all the factors of a number? And put those factors into a list? If you have a field "yourField" with a number in it and a button "yourButton", place this into the button script:
This gives the factors as a list of items. Do you also need help in placing those factors into some form that you find useful? Write back if so.
Craig Newman
Do you mean find all the factors of a number? And put those factors into a list? If you have a field "yourField" with a number in it and a button "yourButton", place this into the button script:
Code: Select all
on mouseUp
factor field "yourField"
end mouseUp
on factor var
put 2 into counter
repeat until counter > var
if (var / counter) is an integer then
put counter & "," after tList
divide var by counter
next repeat
else add 1 to counter
end repeat
answer char 1 to -2 of ("1," & tList)
end factor
Craig Newman
-
- Posts: 4
- Joined: Mon Jan 09, 2012 4:47 pm
Re: Help with placing iterated function results into a list
Thank you! The code is close to what I was planning, and the prime factorization will be helpful in the next stage of what I am doing.
But for now, I was hoping that a person could put a number (like 24) into a field and push a "factor me" button where they would be given a list (1*24, 2*12, 3*8, 4*6) of factors for that number placed into a vertical list format in a field on the screen.
Thanks again for all your help! I look forward to your response!
Carrie
But for now, I was hoping that a person could put a number (like 24) into a field and push a "factor me" button where they would be given a list (1*24, 2*12, 3*8, 4*6) of factors for that number placed into a vertical list format in a field on the screen.
Thanks again for all your help! I look forward to your response!
Carrie
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Help with placing iterated function results into a list
Hmmm.
I am going to give you this, but pretty soon you must play around by yourself. Please let me know if the code so far is completely readable for you, that is, you can follow the script step by step. With fields as you can see in the script, place this in a button
Let me know...
Craig Newman
I am going to give you this, but pretty soon you must play around by yourself. Please let me know if the code so far is completely readable for you, that is, you can follow the script step by step. With fields as you can see in the script, place this in a button
Code: Select all
on mouseUp
factor fld "yourArgumentField"
end mouseUp
on factor var
repeat with y = 1 to round(sqrt(var))
if (var / y) is an integer then
put var/y & "*" & y & return after tList
end if
end repeat
put tList into fld "yourAnswerField"
end factor
Craig Newman
-
- Posts: 4
- Joined: Mon Jan 09, 2012 4:47 pm
Re: Help with placing iterated function results into a list
That is actually very similar to the code I had written (I am a math teacher and my mathematics and logic were correct) I was just unaware of the tList feature. I tried using several different list and menu options in the dictionary but the tList is not in the dictionary. Thanks very much for the help.
Re: Help with placing iterated function results into a list
tList is not a function, so you will not find it in the dictionary. tList is just a variable that Craig is using to capture the factors. t prefix on a variable name is just common livecode naming shorthand to remind the programmer that it is a temporary variable that is local to the function and will not stick around between uses - once the function ends it is erased from memory. The name tList tells everyone that this variable is temporary and is being used to create a list.
This line does the formatting and builds a string in the pretty format that you want to show the user (## * #) then puts a return character after it to end the string. The last part of the line "after tList" actually puts it into the variable for safe keeping until all the factors are found. The "after" is very important to put the new line at the bottom of the list - if you used "into", it would wipe out anything already in the variable, and if you used "before" it would add it to the top of the list.
At the end of the repeat, the tList variable holds all the values, and the line puts the whole thing into the field where it becomes visible to the user.
It would have been overkill for this purpose, but tList could have been an array variable or almost any other container. Or Craig could have put each line into the field as it was found, but inserting into fields is much slower than adding to a variable, so he chose to accumulate everything in memory before he dumped it all at once to the field.
Hope this takes you a bit deeper,
Kelly
Code: Select all
put var/y & "*" & y & return after tList
At the end of the repeat, the tList variable holds all the values, and the line
Code: Select all
put tList into fld "yourAnswerField"
It would have been overkill for this purpose, but tList could have been an array variable or almost any other container. Or Craig could have put each line into the field as it was found, but inserting into fields is much slower than adding to a variable, so he chose to accumulate everything in memory before he dumped it all at once to the field.
Hope this takes you a bit deeper,
Kelly
-
- Posts: 4
- Joined: Mon Jan 09, 2012 4:47 pm
Re: Help with placing iterated function results into a list
Yes! That is very helpful! Thank you!
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Help with placing iterated function results into a list
Kelly has given a very nice mini-tutorial about LiveCode.
I suspected that you were very new to this, and so asked that you examine each line so that you can learn how they worked. Please do so.
That you did not know how to place data in a variable makes me think you might be confused by some of the code lines I wrote, which contain layered elements. The fact that you are a math teacher will likely help you see the intent, now it is time to learn the language. For example, where does "var" come from?
Write back often. This can be fun for everyone.
Craig Newman
I suspected that you were very new to this, and so asked that you examine each line so that you can learn how they worked. Please do so.
That you did not know how to place data in a variable makes me think you might be confused by some of the code lines I wrote, which contain layered elements. The fact that you are a math teacher will likely help you see the intent, now it is time to learn the language. For example, where does "var" come from?
Write back often. This can be fun for everyone.
Craig Newman