How do I finish coding this problem?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Frank Pinedo
am 13 Mai 2019
Kommentiert: Frank Pinedo
am 13 Mai 2019
I am coding a problem from project euler and its problem 52.
I cant seem to go any further and i need help finishing up the problem and making it work on MATLAB.
Problem: It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
My code: MAIN
int begin=1;
bool found=false;
int result=0;
while(not(found))
begin= begin*10;
for i = 0: begin*10/6
found=true;
for j=2:6
out1 = permuted(i,j*i);
if out1, found = false ; break , end
end
if found, result=i; break , end
end
end
function out1 = permuted(x,y)
% ??? int arr[] =[10];
temp = y;
while (temp>0)
arr(mod(temp,10)) + 1;
temp = temp / 10;
end
temp = x;
while(temp > 0)
arr(mod(temp/10)) - 1;
temp = temp / 10;
end
for i=0;10;
if arr(i) ~= 0
out1 = false;
end
end
out1=true;
end
I dont know what else to do can comeone please help me finish the code to complete the problem.
2 Kommentare
Jan
am 13 Mai 2019
I've formatted your message to improve the readability. Some lines of code are not meaningful:
int begin=1; % Not Matlab
bool found=false;
int result=0;
int arr[] =[10]; % Not Matlab
arr(mod(temp/10)) - 1; % This does nothing
for i=0;10; % You mean: for i = 1:10
if arr(i) ~= 0
out1 = false;
end
end
out1=true; % This overwrites out1 in every case
Akzeptierte Antwort
Raj
am 13 Mai 2019
Are you sure your question is answerable? I mean does such a number even exists?
Anyways, I wrote this code and ran it for about an hour before my laptop started to heat up and I had to stop the run. You can try running the code overnight and hope to see some result in the morning. All the best. If you find your magic number please dont forget to post it here.
for num=1:inf
num
A=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
A1=sort(A);
num=2*num;
B=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
B1=sort(B);
num=3*num;
C=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
C1=sort(C);
num=4*num;
D=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
D1=sort(D);
num=5*num;
E=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
E1=sort(E);
num=6*num;
F=arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0);
F1=sort(F);
if numel(A1)==numel(B1)==numel(C1)==numel(D1)==numel(E1)==numel(F1)
if A1==B1==C1==D1==E1==F1
Required_Number = sprintf('%d', A)
break
else
continue
end
end
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!