SPOJ 91 (Two squares or not two squares) optimization in Python -


im trying practice python attempting spoj problems , im kinda stuck on 91st problem

ive used fermat's theorem sum of 2 squares logic of program, keep getting 'time limit exceeded' error when testing solution. heres code:

import sys import math  def factor(n):     d=2     primfac = []      while d*d <= n:         while n%d == 0:             primfac.append(d)             n/=d         d+=1     return primfac  def fun(num):     factors = factor(num)     r=0     prevr = 0     inc=1      if factors == [] , num%4==1:         return 1      f in factors:         if f%2!=0 , f%4==3:             r=f             if r==prevr:                 inc+=1             prevr = f      if inc%2==0:         return 1      return 0   if __name__ == '__main__':     x=0     line in sys.stdin:         if(x==0):             x=1             continue          n = int(line)          if(fun(n) == 1):             print 'yes'         else:             print 'no' 

im guessing there might optimizations , small tweaks can add make code run faster, im clueless are.. please help!

your code, when submitted (python 2.7), yields wrong answer, not time limit exceeded. input value 2 incorrectly prints 'no'.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -