I've stumbled across a problem. I wish to take all the first hundred digits in the square root of 2 ---> sqrt(2) and sum them up.
I first use
which gives me alot of digits. But when I try to num2str the answer it cant be done. How can I achieve this?
No products are associated with this question.
Here you go:
sumA = 0;
for i = 1:100
sumA = sumA + (floor(N*(10^(i-1))) - 10*floor(N*10^(i-2)));
Could you operate on the VPA number instead of converting it to a string? I do not have the Symbolic Toolbox, but I guess:
s = 0; x = vpa(N, 100); for ii = 1:100 s = s + floor(x); x = rem(x, 1) * 10; end
Does this work?
N = sym('sqrt(2)'); x = vpa(N, 100);
s = 0; for ii = 1:100 f = floor(x); s = s + f; x = (x - f) * 10; end
If rounding error appear, try it with 200 digits in the VPA command. Please use this as inspiration - as I said already, I do not have the corresponding toolboxes.
Play games and win prizes!Learn more