Matlab loop not working properly -
largest = 0; num = 0; temp = 0; num_flip = 0; x = 100 : 999 y = x : 999 num = x*y; temp = num2str(num); num_flip = str2double(fliplr(temp)); if num/num_flip == 1 largest = num; 1 = x; 2 = y; end end end
i'm trying find largest palindrome made product of 2 3-digit numbers reason loop stops @ x = 924 , y = 962 know that's not answer. code works fine 2-digit numbers(10 : 99) though.
you not testing if largest largest code. go through loops possible smaller palindrome detected after larger one, , reassigning largest it.
try added test whether new palindrome larger current largest:
largest = 0; num = 0; temp = 0; num_flip = 0; x = 100 : 999 y = x : 999 num = x*y; temp = num2str(num); num_flip = str2double(fliplr(temp)); if ((num/num_flip) == 1) && (num > largest) largest = num; end end end
i removed these assignments
one = x; 2 = y;
because it's not clear question. add them if needed reason not shown.
Comments
Post a Comment