0

I'm trying to read digits of irrational numbers such as 2^0.5 and pi one by one in Matlab. I tried using this:

x=pi;
y=num2str(x)
out=str2num(y(1))

but it only reads just a few digits. Then I tried using vpa function.

x=vpa(pi,100);
y=num2str(x)
out=str2num(y(1))

but vpa makes x a sym variable which num2str can't read.

Then I tried the method explained in here, but it only seems to work for rational numbers since

sym(99)^95

returns a number which can be read digit by digit using

char(sym(99)^95)-'0'

but

sym(2)^0.5

just says

ans= 2^(1/2)

How can I do the same with irrational numbers to say 10000 digits of precision and store these digits in a (1000,1) array?

Alireza
  • 410
  • 3
  • 17
  • 1
    That is very very non-trivial for large decimals, you definitely need symbolic math. Generally speaking, people propose infinite series that converge to the number at hand and compute it in parts (as partly explained [here](https://math.stackexchange.com/questions/297/simple-numerical-methods-for-calculating-the-digits-of-pi)) – Ander Biguri Nov 08 '18 at 09:57
  • @AnderBiguri It is a method I thought of at first but I need exact digits in this range. even a +-1 uncertainty in the 10000th digit would not work. – Alireza Nov 08 '18 at 10:15
  • 1
    This type of methods have been used to compute **trillions** of digits. – Ander Biguri Nov 08 '18 at 10:15
  • 1
    To be more precise, 42*10^8 digits of Pi: http://www.super-computing.org/pi_current.html . This page shows the current 2nd world record of computations of Pi. – Ander Biguri Nov 08 '18 at 10:18
  • @AnderBiguri That would work. So I'll try using char(sym(1-1/3+1/5-1/7+1/9-1/11+...)-'0') to get close to digits of pi/4. – Alireza Nov 08 '18 at 10:22
  • let me be clearer than my previous comments: The 2nd world record used code 78000 lines long to be able to do this accurately. Your problem is not a simple problem, its an entire research project. You can not "just ask it in the internet" – Ander Biguri Nov 08 '18 at 10:22
  • @AnderBiguri Well I thought of it as a "programming issue for which there is a rather simple solution because doing the same for 99^95 is simple" and maybe I was wrong. How did I found out? by "asking on the internet". – Alireza Nov 08 '18 at 10:27
  • 1
    Apologies, I did not mean to be rude, but informative. Just letting you know you have a bigger problem than you think you had! ;) – Ander Biguri Nov 08 '18 at 10:28
  • 1
    Even more interesting for you, the world record holder in 2013 explains a bit further how they did it. https://stackoverflow.com/questions/14283270/how-do-i-determine-whether-my-calculation-of-pi-is-accurate – Ander Biguri Nov 08 '18 at 10:29
  • @AnderBiguri It was really helpful. Actually, I was trying to do a small project of numerology for my own curiosity. Seems it is harder than what I thought. Thank you. – Alireza Nov 08 '18 at 10:31
  • 2
    @AnderBiguri Note that there are some relatively simple algorithms to compute the digit-by-digit decimal value of square roots, see [here](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Digit-by-digit_calculation). Your point stands, but 78,000 lines of code is definitely not needed if you limit the problem to, for example, square roots. A [Project Euler question](https://projecteuler.net/problem=80) asks for arbitrary precision calcs of root 2, for which many solutions can be found online (e.g. [here](https://blog.dreamshire.com/project-euler-80-solution/)) – Wolfie Nov 08 '18 at 11:27

0 Answers0