Filter löschen
Filter löschen

Champernowne Constant loop help

1 Ansicht (letzte 30 Tage)
Nguyen Huy
Nguyen Huy am 15 Mai 2021
Bearbeitet: Jonas am 16 Mai 2021
%i have Champernowne Constant is a real number whose digits in decimal representation come from the concatenation of all consecutive positive integers starting from 1. ex:'12345678910111213....'
%my job is return array vy contain nth digit from Champernowne Constant,n is take from array vx
%ex: vx=[10 11 12 13 14 15] => vy=[ 1 0 1 1 1 2]
%my code is
vy=[]
for i=1:length(vx)
str='';
j=1;
while length(str)<vx(i)
str=strcat(str,num2str(j));
j=j+1;
end
vy=[vy str2num(str(vx(i)))];
end
%the code is success with litte vx,but with lagre vx it over max loop of matlab,so how could i fix
  1 Kommentar
Jonas
Jonas am 16 Mai 2021
Bearbeitet: Jonas am 16 Mai 2021
maybe it helps if you have a look into the width of numbers: the numbers from 0:9 have size ceil(log10(number))=1, after that the numbers from 10:99 have width ceil(log10(numbers))=2 and so on. this way you could have a look only on the numbers in the decade you are interested in.
by the way, how big gets your n? you can easily use smth like
sprintf('%i',1:10000)
and then extract the numbers you are interested in?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by