about product in uint64

1 次查看(过去 30 天)
Nguyen Huy
Nguyen Huy 2021-5-19
编辑: Jan 2021-5-20
My question is iven an integer x which contains d digits, find the value of n (n > 1) such that the last d digits of x^n is equal to x. If the last d digits will never equal x, return inf.
Example :
%x = 2; (therefore d = 1)
%2^2 = 4, 2^3 = 8, 2^4 = 16, 2^5 = 32
%n = 5;
So my solution is take x into uint64 vecto,then take the x^n by take product of a*x with example is
a=uint64([x])
for i=1:inf
a=a*x
for j=length(a):-1:1
b=rem(a(j)/10)
c=mod(a(j),10)
if a(j)>10
a(j)=c
a(j-1)=a(j-1)+b
end
end
if x==a(end-d:end)
break
end
end
%example : 0 0 0 2 *
2
==========
0 0 0 4 => [0 0 0 4]*2 =>[0 0 0 8]*2=>[0 0 0 16]=[0 0 1 6]*2=>[0 0 0 32]=[0 0 3 2] have a(end-d:end)==x then break
so my question is how could i product the number with over 2 digits in uint64()
  1 个评论
Jan
Jan 2021-5-20
Why do you use uint64 instead of uint8 to store decimal digits?
What about limiting the solution to the UINT64 range, which is at least up to 1.8e19?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2021-5-20
编辑:Jan 2021-5-20
With limiting the values to UINT64 range:
x = uint64(2);
d10 = uint64(10^(floor(log10(x)) + 1));
n = 2;
a = x^n;
lim = intmax('uint64');
while rem(a, d10) ~= x && a <= lim
a = a * x;
n = n + 1;
end
if a < lim
disp(n)
else
disp('search failed.');
end

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by