Project Euler #216: Investigating the primality of numbers of the form 2n² - 1

Sort by

recency

|

20 Discussions

|

  • + 0 comments
    import math
    p=list(map(int,input().split()))
    a=p[0]
    b=p[1]
    c=p[2]
    d=0
    for _ in range(int(input())):
        n=int(input())
        for i in range(0,n+1):
            t=(a*(i**2)+b*(i)+c)
            if t<0:
                t*=(-1)
            if t>=2:
                flag=1
                for j in range(2,int(math.sqrt(t))+1):
                    if t%j==0:
                        flag=0
                        break
                if flag==1:
                    d+=1
        print(d)
        d=0
    

    normal solution

  • + 0 comments

    what is the meaning of number of queries in this question?

  • + 0 comments

    this is my code. I used sieve of eratosthenes to get possible primes till the Nth value. passed basic test cases but getting wrong answer in others can anyone share robust test cases or any other approach?

    #python code
    import math
    def getPrimeCount(N, abc):
        a = int(abc[0])
        b = int(abc[1])
        c = int(abc[2])
        nth = a*(N**2) + b*N + c
    
        primeSieve = [True]*(nth + 1)
        primeSieve[0] = False
        primeSieve[1] = False
    
        count = 0
        root = math.floor(math.sqrt(nth))
        i=2
        while i<= root:
            if primeSieve[i] == True:
                for j in range(i*i, nth+1, i):
                    primeSieve[j] = False
            i += 1
        
        for itr in range(N+1):
            val = a*(itr**2) + b*itr + c
            if val > 1 and primeSieve[val]:
                count += 1
    
        return count
    
    abc = input().split()
    queries = int(input())
    for _ in range(queries):
        n = int(input())
        print(getPrimeCount(n, abc))
    
  • + 0 comments

    my three test case are running well but others are showing timeout error!! ????????????????????

  • + 1 comment

    can anyone pls help me in timeout error