Home
Home
Apps
Apps
Ask
Ask
Manual
Manual
Share
Share

Integer-to-digits


#1

Is there a function to convert an integer to a list of its digits?


#2
n=4711
s_digit = string(n)->[]
n_digit = char(string(n))-48

in witch …

  • string(n) converts n to a string, similar “”<<n
  • char splits a string to a list of ASCII-Codes
  • s_digit is a list of strings “4”, “7”, “1”, “1” and n_digit is a list of number 4,7,1,1.

#3

again: string(n) = ""<<n = "4711"


#4

Robert-
This is IMMENSELY helpful! Is there any way I can insure that all my questions get to YOU? (You’ve petted a stray cat.)
Thanks,
Abijah Reed


#5

Hello abijah,
thank you for the nice answer. I am new to mathstud.io but plan to check the discussion forum regularly. If I see a question from you, I will do my best. Best regards, Robert


#6

Hi, Robert
I need some help understanding how to create and use (call) subroutines with both input and output arguments. I think it involves scripts, include statements, and compile statements, but I don’t seem to be able to find clear examples in the documentation. Want to have a go?
With appreciation & thanks,
Abijah Reed


#7

Hello abijah,

you can define functions by “@functionname” within a file and call dem by “functionname” in the same file. There is no need to compile them. This usage is explained in the tutorial section by Shakey (Share/Tutorials/Beginner Tutorial Variables and Functions). I think that is easy to understand.

I will try to explain what it is with the compiled functions by way of example:

Suppose you miss a few functions in the Mathstudio commands. Suppose these are the functions “Convert number to a list of digits” and “Reverse the order of digits in a number”. You want to use these function in many cases without having to reprogram them every time. In this case, you have to proceed as follows:

  1. Create a new file with e.g. the name “MyTools”. This file
    contains the two functions:

    @NumToList(Number n)
    L = char(string(n))-48
    Return(L)

and

@NumReverse(Number n)
L=NumToList(n)
r=0
loop(i,Size(L),1,-1)
  r=r+L(i)*10^(i-1)
end
Return(r)

In both cases, the input value is a number ("Number n"). In the first case, the output value is a list ("Return (L)") and in the second case the output value is a Number ("Return (r)"). The second function uses the first function. Both functions can be tested within the file “MyTools” e.g. by enter:

NumToList (4711)
NumReverse (4711)

You can also do more complex functions with multiple inputs and outputs by writing:

@AnyFunction (a, b, c, d, e)
...
... do anything with a, b, c, d, e
... and compute x, y, z
...
Return (x, y, z)

In the concrete example, the story goes on as follows:

  1. Save the file “MyTools” with the two tested functions. This
    is the source code and the file is called “MyTools.math”.

  2. This source code is compiled as follows:

Mac:
Mathstudio Menu / File / Evaluate all
Mathstudio Menu / File / Export scripts to include folder

iOS:
File Icon / Compile scripts

  1. In both cases, a so-called compiled file with name “MyTools.script” is generated (located in the “include folder” in case of Mac and in the same directory as MyTools.math in case of iOS).

  2. Close the file “MyTools.math” and you are ready!

If you want to use the new functions in another file, then you proceed as follows:

a) Create any file, e.g. “Testfile”
b) At the beginning of this file you write: include ("MyTools")
c) After pressing “return” the function names “NumToList
and “NumReverse” appear.
d) These functions you can now use in “Testfile” like normal
Mathstudio functions.
e) For example: NumToList (1234) or NumReverse (12345)

So this is a elegant way, to expand Mathstudio to your own needs.

Remarks:
If you want to change the functions “NumToList” or “NumReverse” or want to add more features / other functions, then you have to make these changes in the source code “MyTools.math” and after that

  • Testing
  • Save MyTools.math
  • recompile again

Hope this helps …
Best regards
Robert


#8

Robert-
I can’t thank you enough for your elaborate reply to my appeal for help!
I have an Android phone, and I’m eager to follow your examples and see how (and whether) they work.
Thanks again,
Abijah