So I'm new to SPOJ, and i've got this non-zero exit code... this is the code

program PALIN;
var n,i,pal:longint;
function nxtpal(a:longint):longint;
var tpal,t:longint;
begin
        tpal:=a;
        t:=0;
        while (tpal>0) do begin
                t:= (t*10) + (tpal mod 10);
                tpal:=tpal div 10;
        end;
        if (a <> t) then a:=nxtpal(a+1);
    nxtpal:=a;
end;
begin
        readln(n);
        for i:=1 to n do begin
                readln(pal);
                //if (pal<10) then writeln(11)
                //else begin
                        pal:=nxtpal(pal+1);
                        writeln(pal);
                //end;
        end;
end.

What does this code actually done is that the [color=#BF4000]function nxtpal[/color] is searching for the next palindrome number starting from (n+1), and the [color=#BF4000]commented section[/color] is because in the comment some people says for n=1 to 9 the output should be 11. As what i saw nothing is wrong with my code, but i always got NZEC, when i run it on ideone.com it got compiled and run properly, producing the right answer... what's wrong with my code? is something not possible done in SPOJ?

  • created

    Mar '15
  • last reply

    Mar '15
  • 1

    reply

  • 1.2k

    views

  • 2

    users

The largest value that will fit inside longint in pascal is 2^31-1. The largest value that colud be seen in the input is 10^1000000. You need to find another way to find this solution.

Your algorithm is also too slow. This will take a long time to run, and it's not close to 10^6 digits long.

1
1000000000100000000001

Try to think a little bit more about what a palindrome actually is. It might even be easier if you completely ignored the fact that the input is a number.