필터 지우기
필터 지우기

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에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by