9
$\begingroup$

My apologies if this was asked here before.

My friend and I want to do this:

Suppose that we have some real number $a. a_1 a_2...a_n...$ where $a_i$ are digits in the set $\{1,2,3,4,5,6,7,8,9,0\}$ (so we are in the world of decimal expansion).

We can discard non-decimal-part digit and turn it into 0 so we have now number $0. a_1 a_2...a_n...$

From this number we want to make a sequence of natural numbers that is defined in this way:

$a_1,a_1 a_2,a_1 a_2 a_3, a_1 a_2 a_3 a_4...$

That is, we want from decimal part of a real number to take first digit as one-digit natural number which is first in the new sequence, first two as two-digit natural number which is second in the new sequence, and so on...

As an example, suppose that we have real number $0.123456345436...$. We want that Mathematica take that real number as an input and that gives us output (with commas, of course):

$1,12,123,1234,12345,123456,1234563,12345634...$

How to do that in Mathematica?

$\endgroup$

5 Answers 5

7
$\begingroup$

You may use FractionalPart to discard the integer portion of the number. Then FoldList using FromDigits on the RealDigits.

Without leading zeros

With

num = N[Pi, 5]
3.1416

Then

FoldList[FromDigits[{##}] &]@First@RealDigits@FractionalPart@num
{1, 14, 141, 1416}

With leading zeros

If you need the leading zeros in the decimal part then you need to PadLeft the RealDigits with zeros.

With

num = 1.00234`6;

Then

FoldList[FromDigits[{##}] &]@*(PadLeft[#1, Length@#1 + Abs[#2]] &) @@ 
 RealDigits@FractionalPart@num
{0, 0, 2, 23, 234}

Hope this helps.

$\endgroup$
4
$\begingroup$

Here it is for the first 50 digits of Pi:

dec = Mod[N[Pi, 50], 1];
digits = RealDigits[dec][[1]]; 
Table[N[FromDigits[digits[[1 ;; i]]]/10^i, i], {i, 1 Length[digits]}]

{0.1, 0.14, 0.141, 0.1415, 0.14159, 0.141592, 0.1415926, 0.14159265,  0.141592653, 
 0.1415926535, 0.14159265358, 0.141592653589, 0.1415926535897, 0.14159265358979...

To "remove the zero and dot", use (incorporating a simplification suggested by Carl Woll in the comments)

Table[FromDigits[digits[[1 ;; i]]], {i, 1 Length[digits]}]

{1, 14, 141, 1415, 14159, 141592, 1415926, 14159265, 141592653, 1415926535, 
   14159265358, 141592653589...

You can do it without Table as well, for a more functional feel.

FromDigits[digits[[1 ;; #]]] & /@ Range[Length[digits]]

(outputs truncated because they're long).

$\endgroup$
6
  • $\begingroup$ Can you remove $0$ and the dot? $\endgroup$
    – user52558
    Sep 29, 2017 at 20:55
  • $\begingroup$ With some command. $\endgroup$
    – user52558
    Sep 29, 2017 at 20:55
  • $\begingroup$ See update to make them integers. $\endgroup$
    – bill s
    Sep 29, 2017 at 20:58
  • $\begingroup$ If I were to use some other number instead of apple $\pi$ should I write it in the place where it is written Pi, will program then also work? $\endgroup$
    – user52558
    Sep 29, 2017 at 21:02
  • 2
    $\begingroup$ Just replace Pi with your favorite number. But really, why not try it and see? $\endgroup$
    – bill s
    Sep 29, 2017 at 21:04
3
$\begingroup$

Hopefully the code is self-explanatory. The key is RealDigits.

Clear[seq]
seq[x_, n_: Automatic] :=
 Module[{digs, p},
  {digs, p} = RealDigits[x, 10, n];
  digs = Drop[digs, p];
  Table[FromDigits@Take[digs, k], {k, Length[digs]}]
  ]

seq[Pi, 10]
(* {1, 14, 141, 1415, 14159, 141592, 1415926, 14159265, 141592653} *)

seq[N[Pi]]
(* {1, 14, 141, 1415, 14159, 141592, 1415926, 14159265, \
141592653, 1415926535, 14159265358, 141592653589, 1415926535897, \
14159265358979, 141592653589793} *)
$\endgroup$
3
$\begingroup$
  number = 2.643512356265;
FromDigits@Take[List @@ RealDigits[number - Floor@number][[1]], #] & /@
  Range[Length[List @@ RealDigits[number][[1]]]]
$\endgroup$
3
$\begingroup$
ClearAll[decimalParts]
decimalParts = Module[{decdigits = RealDigits[FractionalPart@#][[1]]}, 
    Extract[decdigits, List /@ Range[Range[Length@decdigits]], FromDigits]] &;

x = 2.123456345436;
decimalParts[x]

{1, 12, 123, 1234, 12345, 123456, 1234563, 12345634, 123456345, 1234563454, 12345634543, 123456345435, 1234563454359, 12345634543599, 123456345435999, 1234563454359998}

decimalParts[N[Pi, 10]]

{1, 14, 141, 1415, 14159, 141592, 1415926, 14159265, 141592653}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.