How to increase precision in this case?

1 次查看(过去 30 天)
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?

采纳的回答

John D'Errico
John D'Errico 2022-10-19
编辑:John D'Errico 2022-10-19
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 个评论
John D'Errico
John D'Errico 2022-10-19
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 2022-10-19
编辑:Daniel 2022-10-19
Thank you John. We learned only loops (for, while) on the last lesson. I didnt know about it

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by