optimised but not working for large no like 999999