How to increase precision in this case?

2 views (last 30 days)
I have a code which finds the first 10 consecutive numbers after decimal that forms a prime number:
k = 10;
while true
a = mod(floor((pi-3)*10^k),10^10);
if isprime(a) == 1
disp(a)
break
endif
k += 1;
endwhile
And this only works for pi, I also need it to e but its not enough precision
What can i do here?

Accepted Answer

John D'Errico
John D'Errico on 19 Oct 2022
Edited: John D'Errico on 19 Oct 2022
Take it from here:
vpa(exp(sym(1)),500)
ans = 
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931
Now just process the digits.
Or I think I have something on the order of a million digits of e and pi stored in my HPF toolbox. (Might be only a half million.)
  4 Comments
John D'Errico
John D'Errico on 19 Oct 2022
Can you extract the string of digits? I think you are fixated on the idea that you NEED to work with numbers, and there is only one possible way to extract those digits. That is completely wrong. I showed you how to extract all the digits you want. Perhaps I stopped one step short, thinking you could take it from there. So I'll add a next step.
D = char(vpa(exp(sym(1)),500))
D = '2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931'
That is not a string of numeric digits there, but a string of characters. But even so, it is all you need. Feel free to delete the decimal point even.
D(2) = []
D = '27182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931'
You can even convert them into actual numeric digits if you feel the need, but you need to make some effort, and I'm sorry, but I won't do your homework, and I've already done almost all of it here.
Daniel
Daniel on 19 Oct 2022
Edited: Daniel on 19 Oct 2022
Thank you John. We learned only loops (for, while) on the last lesson. I didnt know about it

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!