-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem026.asv
More file actions
45 lines (41 loc) · 1.71 KB
/
problem026.asv
File metadata and controls
45 lines (41 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function d = problem026(n)
%A unit fraction contains 1 in the numerator.
%Let 0.1(6) means 0.166666..., and has a 1-digit recurring cycle.
%It can be seen that 1/7 has a 6-digit recurring cycle.i.e 1/7 = 0.(142857)
%Find the value of d < 1000(or n in general) for which 1/d contains the longest recurring cycle
%in its decimal fraction part.
%The function primes_to_n.m should be defined in the directory.
%The function factors_to_n.m should be defined in the directory.
%The function digtial_factors.m should be definded in the directory
%If p is prime and a is any integer, then ap ? a is divisible by p ( Fermat's little theorem).
%If p is a prime number other than 2 and 5, 1/p is always a recurring decimal,
%whose period is p?1 or a divisor of p?1. This can be deduced directly from Fermat's little theorem.
% so all we have to do is to find primes to n, then for every prime p we
% have to calculate all the factors of p-1 then, the period of recurrence
% has to be one of the proper devisior of it
%%++++ BUT THIS FLOW WILL NOT WORK. BECAUSE THE PRECETION OF DIGITS IS 17
%%ONLY. AND HENCE WE HAVE TO CHECK REGAROUSLY.THE ONLY POINT IS CLEAR FROM
%%THIS DISCOSSION IS THAT LONGEST RECURRING CYCLE WILL HAPPEN AT PRIME
%%NUMBERS ONLY
B = primes_to_n(n);
bn = length(B);
C = zeros(1,bn);
for i = 4:bn % 2 and 5 are excluded bcos they dont form recurring decimal
j = 2;k = B(1,i)-1;
while A(B(1,i)-1,j) ~= 0
t = rem(10^A(B(1,i)-1,j),B(1,i));
if t == 1
if A(B(1,i)-1,j) <= k
k = A(B(1,i)-1,j);
end
end
j = j +1;
if j > an
break
end
end
C(1,i) = k;
end
[v1 i1] = max(C);
C
d = B(1,i1);