Page 1 of 1
					
				Math problem
				Posted: Wed Feb 05, 2014 9:23 am
				by carel
				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
			 
			
					
				Re: Math problem
				Posted: Wed Feb 05, 2014 11:55 am
				by jmburnod
				Hi Carel,
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
best regards
Jean-Marc
 
			
					
				Re: Math problem
				Posted: Wed Feb 05, 2014 4:40 pm
				by carel
				Jean-Marc,
Thank you, I would've had a hundred lines of sloppy code to get the same result 
 
Carel
 
			
					
				Re: Math problem
				Posted: Thu Feb 06, 2014 3:52 pm
				by Thierry
				Hi,
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
or:
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
So many ways..  
 
Regards,
Thierry
 
			
					
				Re: Math problem
				Posted: Thu Feb 06, 2014 6:26 pm
				by carel
				Cool Thierry - now I learned two more things as well... format and after
Thanks,
Carel
			 
			
					
				Re: Math problem
				Posted: Thu Feb 06, 2014 6:39 pm
				by Thierry
				carel wrote:Cool Thierry - now I learned two more things as well... format and after
l
Umm, "after" was already in JM post 
 
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)
transformed to:
Code: Select all
char i of pNumber * item i of tMultiple
Thierry
 
			
					
				Re: Math problem
				Posted: Fri Feb 07, 2014 8:18 am
				by carel
				Thierry,
Can't you make it any shorter? - That is so much typing to do.
Just kidding 
 
Thanks,
Carel
 
			
					
				Re: Math problem
				Posted: Fri Feb 07, 2014 8:23 am
				by Thierry
				Sure, I can!
Enjoy your day 
 
Thierry
 
			
					
				Re: Math problem
				Posted: Fri Feb 07, 2014 10:18 am
				by Thierry
				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
Votre serviteur,
just kidding 

 ... but it works too!
Regards,
Thierry