Math problem
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Math problem
I'm writing something for my son to help him (test him) with his math homework.
He has to break numbers down into thousands, hundreds, etc.
For example: he has a number 1234, then he must say the thousands are 1000, hundreds 200, tens 30, and units 4.
I'm thinking of counting the number of characters, then use the last three (234) to subtract to get the thousands, then the last two (34) to subtract from the 234 to get the hundreds, and the 4 from 30 for the tens, and then just use the 4.
But I'm sure you clever people has a much more elegant solution?
Thanks,
Carel
He has to break numbers down into thousands, hundreds, etc.
For example: he has a number 1234, then he must say the thousands are 1000, hundreds 200, tens 30, and units 4.
I'm thinking of counting the number of characters, then use the last three (234) to subtract to get the thousands, then the last two (34) to subtract from the 234 to get the hundreds, and the 4 from 30 for the tens, and then just use the 4.
But I'm sure you clever people has a much more elegant solution?
Thanks,
Carel
Re: Math problem
Hi Carel,
Here is an other way by a function
best regards
Jean-Marc
Here is an other way by a function
Code: Select all
on mouseUp
put AnalyseNumber(1234)
end mouseUp
function AnalyseNumber pNumber
put empty into rAnalyseNumber
put "thousands,hundreds,tens,units" into tCol
put "1000,100,10,1" into tMultiple
repeat with i = 1 to the length of pNumber
get (the value of char i of pNumber * the value of item i of tMultiple)
put item i of tCol && "=" && it & cr after rAnalyseNumber
end repeat
delete char -1 of rAnalyseNumber
return rAnalyseNumber
end AnalyseNumber
Jean-Marc
https://alternatic.ch
Re: Math problem
Jean-Marc,
Thank you, I would've had a hundred lines of sloppy code to get the same result
Carel
Thank you, I would've had a hundred lines of sloppy code to get the same result

Carel
Re: Math problem
Hi,
Here is 2 variations of Jean-Marc's code:
or:
So many ways.. 
Regards,
Thierry
Here is 2 variations of Jean-Marc's code:
Code: Select all
function AnalyseNumber pNumber
put empty into Rslt
put "thousands,hundreds,tens,units" into tCol
put "1000,100,10,1" into tMultiple
repeat with i = 1 to the length of pNumber
get (char i of pNumber) * item i of tMultiple
put item i of tCol & " = " & it & cr after Rslt
end repeat
return Rslt
end AnalyseNumber
Code: Select all
function AnalyseNumber pNumber
local Rslt
local tCol = "thousands,hundreds,tens,units"
local tMultiple = "1000,100,10,1"
repeat with i = 1 to the length of pNumber
put format("%s = %d\n", item i of tCol, \
char i of pNumber * item i of tMultiple ) after Rslt
end repeat
return Rslt
end AnalyseNumber

Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Math problem
Cool Thierry - now I learned two more things as well... format and after
Thanks,
Carel
Thanks,
Carel
Re: Math problem
Umm, "after" was already in JM postcarel wrote:Cool Thierry - now I learned two more things as well... format and after
l

My main idea to post here was mainly because of this line:
Code: Select all
get (the value of char i of pNumber * the value of item i of tMultiple)
Code: Select all
char i of pNumber * item i of tMultiple
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Math problem
Thierry,
Can't you make it any shorter? - That is so much typing to do.
Just kidding
Thanks,
Carel
Can't you make it any shorter? - That is so much typing to do.
Just kidding

Thanks,
Carel
Re: Math problem
Sure, I can!
Enjoy your day
Thierry
Enjoy your day

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Math problem
carel wrote:Thierry,
Can't you make it any shorter? - That is so much typing to do.
Code: Select all
local cCol = "units,tens,hundreds,thousands"
function AnalyseNumber N, R
put length(N) into L
if L < 1 then return R
return AnalyseNumber( char 2 to -1 of N, \
R & format("%s = %d\n",item L of cCol,N-(N mod 10^(L-1))))
end AnalyseNumber
just kidding

Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!