-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem037.m
More file actions
81 lines (72 loc) · 2.09 KB
/
problem037.m
File metadata and controls
81 lines (72 loc) · 2.09 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function C = problem037(B)
% The number 3797 has an interesting property.
%Being prime itself, it is possible to continuously remove digits from left to right,
%and remain prime at each stage: 3797, 797, 97, and 7.
%Similarly we can work from right to left: 3797, 379, 37, and 3.
% Find the sum of the only eleven primes that are both truncatable from
% left to right and right to left.
% NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
%The function digital_factors.m should be defined in the directory
%B the set of prime nos upto 1000000 is taken as an input argument
%The number can start with (2/3/5/7) and should end with (3,7)
% We will generate primes below some large nos say 1000000,
%Then check for every prime by digital factorising it
%B = primes_to_n(1000000);
bn = length(B);
j = 5;%for 23 37 53 73
%S = 186;%for 23+37+53+73
C = [];
i = 26;% we will start inspecting numbers above 100,and 26th prime is 101
C1 = [ 2 3 5 7];% to check the starting digit in number
C2 = [ 3 7]; % to check the last digit in number
while i <= bn
F = digital_factors(B(1,i));
t1 = size((find(C1==F(1,1))),2);
t2 = size((find(C2==F(1,end))),2);
if t1 == 1 && t2 == 1
cp1 = left_trunc(F);
if cp1 == 1
cp2 = right_trunc(F);
if cp2 == 1
C =[ C ;B(1,i)];
end
end
end
i = i+1;
end
function cp = left_trunc(F)
%return 1 is if the prime in left truncable else return 0
fl = length(F);
cp = 1;
while fl > 1
F(:,end) = [];
fl = length(F);
N =0;
for i = 0:fl-1
N = N+F(fl-i)*10^i;
end
if is_it_prime(N) == 1
cp =1;
else
cp = 0;
break
end
end
function cp = right_trunc(F)
%return 1 is if the prime in left truncable else return 0
fl = length(F);
cp = 1;
while fl > 1
F(:,1) = [];
fl = length(F);
N =0;
for i = 0:fl-1
N = N+F(fl-i)*10^i;
end
if is_it_prime(N) == 1
cp =1;
else
cp = 0;
break
end
end