Largest Palindrome

Matlab. This is the problem 4 in Project Euler. I made some simple mistakes while trying to write an efficient algorithm. There were two inputs; digit number of the integer and limit number.

For example, the answer would be 198891 for dig = 3 and lim = 200000.

Question: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Write a function that is called this way: >> n = palin_product(dig,lim); The function returns the largest palindrome smaller than lim that is the product of two dig digit numbers. If no such number exists, the function returns 0. (Inspired by Project Euler.)

Solution: Firstly, I created a product matrix of two numbers after describing their ranges based on their digits. As a small step, function checked if square of min number is not smaller than limit or not. Limit was applied and new array was sorted from top to bottom. Then, reverse of the product array was created. As a final stage, for-loop checked if there was any palindromic number in the array. First palindromic number coming in the loop became the largest one.