Math problem

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
carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Math problem

Post by carel » Wed Feb 05, 2014 9:23 am

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Math problem

Post by jmburnod » Wed Feb 05, 2014 11:55 am

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
https://alternatic.ch

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Math problem

Post by carel » Wed Feb 05, 2014 4:40 pm

Jean-Marc,

Thank you, I would've had a hundred lines of sloppy code to get the same result :)

Carel

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Math problem

Post by Thierry » Thu Feb 06, 2014 3:52 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Math problem

Post by carel » Thu Feb 06, 2014 6:26 pm

Cool Thierry - now I learned two more things as well... format and after

Thanks,

Carel

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Math problem

Post by Thierry » Thu Feb 06, 2014 6:39 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Math problem

Post by carel » Fri Feb 07, 2014 8:18 am

Thierry,

Can't you make it any shorter? - That is so much typing to do.

Just kidding :)

Thanks,

Carel

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Math problem

Post by Thierry » Fri Feb 07, 2014 8:23 am

Sure, I can!

Enjoy your day :)

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Math problem

Post by Thierry » Fri Feb 07, 2014 10:18 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply