# I dont know where i was wrong

3 views (last 30 days)
Nguyen Huy on 11 May 2021
Commented: Nguyen Huy on 11 May 2021
%I am doing project euler 8,this is my code
function y = euler008(x)
x=num2str(x)
d=[]
for i=1:length(x)
number = str2num(x(i:i+4))
digits = num2str(number) - '0'
d=[d prod(digits)]
end
y=max(d)
end
%i dont know why this code is wrong

Nguyen Huy on 11 May 2021
Edited: Nguyen Huy on 11 May 2021
i have edited all these errors,but it get %Index exceeds matrix dimensions error
##### 2 CommentsShowHide 1 older comment
Nguyen Huy on 11 May 2021
oh,i forgot that,thanks u all

Atsushi Ueno on 11 May 2021
There is a variable 'a' in caller workspase and a function euler008(a) is called, but argument in the function euler008(x) is 'x'.
number = str2num(a(i:i+4)
shall be changed to
number = str2num(x(i:i+4))

Atsushi Ueno on 11 May 2021
You are trying to access to variable x's index from 1 (min) to length(x)+4 (max).
for i=1:length(x)
number = str2num(x(i:i+4))
The maximum index number of variable x has been exceeded. If you modify the code to the following, it will work correctly.
for i=1:length(x)-4
number = str2num(x(i:i+4))

Atsushi Ueno on 11 May 2021
>The large number will be given as a string, 1xn characters.
So, x=num2str(x) on 2nd line won't work correctly. It should be deleted.