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 Kommentar

Jan
Jan am 20 Mai 2021
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 20 Mai 2021
Bearbeitet: Jan am 20 Mai 2021

0 Stimmen

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

Kategorien

Tags

Gefragt:

am 19 Mai 2021

Bearbeitet:

Jan
am 20 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by