Project Euler #145: How many reversible numbers are there below one-billion?

Sort by

recency

|

64 Discussions

|

  • + 0 comments

    Solved this after 3 days of analysis. Algorithm must be O(logN), otherwise it will time out. I process number digit by digit and update the count value. I created auxilarry arrays with values like: - number of reversible numbers for given number of digits and its cumulative sum - number of odd digit sums without carry over (exluding digit 0) and its cumulative sums - number of odd digit sum without carry over (including digit 0) and its cumulative sums - number of odd digits sums with carry over and its cumultaive sums - number of even digit sums without carryover and its cumulative sums

  • + 0 comments

    Time Out Error

    def is_reversible(n):
        if n % 10 == 0:  # Avoid leading zeroes in the reverse
            return False
        
        sum_n_reverse = n + int(str(n)[::-1])  # Sum of n and its reverse
        for digit in str(sum_n_reverse):
            if int(digit) % 2 == 0:  # Check for even digits in the sum
                return False
        return True
    
    def count_reversible_numbers(limit):
        count = 0
        for i in range(1, limit):
            if is_reversible(i):
                count += 1
        return count
    
    def main():
        test_cases = int(input())
        for _ in range(test_cases):
            limit = int(input())
            reversible_count = count_reversible_numbers(limit)
            print(reversible_count)
    
    if __name__ == "__main__":
        main()
    
  • + 0 comments

    For those who are struggling solving this. At one time it took me about a week of work to solve this problem, may be longer. This is no way an easy thing, and I doubt the solution can be written as one function. Particularly, my solution contains near 600 lines and 17 helper functions.

  • + 0 comments

    Time out can anyone fix it

    n = [int(input()) for i in range(0,int(input()))]
    def reverse(x):
        return int(str(x)[::-1])
    def isInputOdd(x):
        lis = str(x)
        for i in lis:
            if int(i)%2==0:
                flag = False
                return flag
            else:
                flag = True
        return flag
    def main(iter):    
        sum1=0
        for i in range(iter):
            k = i + reverse(i)
            if i%10!=0 and isInputOdd(k):
                sum1+=1
        print (sum1)
    for i in n:
        main(i)
    
  • + 2 comments

    Facing timeout problem... !! def procedure(n): s=str(n) #print("s = ",s) s1 = s[::-1] if int(s1)>10: #print("s1 = ",s1) #print(int(s1)) s2=n+int(s1) #print("s2 = ",s2) if '2' not in str(s2) and '4' not in str(s2) and'6' not in str(s2) and'8' not in str(s2) and'0' not in str(s2): return True else: return False

    t=int(input())#no of test cases while t>0: n=int(input()) c=0 for i in range(10,n): if procedure(i)==True: c=c+1

    print(c)
    t=t-1
    

    Time Complexity :--- O(no.of test cases* n).. Suggest me ...some alternatives.. !!