Instantly share code, notes, and snippets.

# goulu/fermat.py

Last active October 8, 2016 19:54
Show Gist options
• Save goulu/e92cd35c23c921e55c16 to your computer and use it in GitHub Desktop.
Generate (approximate) counter examples of Fermat's Theorem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #!/usr/bin/env python # coding: utf8 """ generate wrong counter-examples of Fermat's theorem see http://www.drgoulu.com/2016/03/25/contre-exemples-au-theoreme-de-fermat-wiles/ """ from __future__ import print_function, division import itertools __author__ = "Philippe Guglielmetti" def check_fermat(a,b,p,m=[2,3,5]): """ :param a,b,p: integers :param m: list of integer (primes) to check for modularity correctness :return: integer c and float relative precision of a^p+b^p=c^p or (false,integer) if sum is wrong modulo m """ left=float(a**p+b**p) c=int(round(left**(1/p))) if c==a: # b is too small return False,0 for m in m: #parity check if (a%m + b%m)%m != c%m: return False,m right=float(c**p) e=abs(left-right)/left # relative precision return c,e print(check_fermat(1782,1841,12,[])) # 1922 Cohen, Simpsons print(check_fermat(3987,4365,12,[2])) # 4472 Cohen, Simpsons print(check_fermat(48767,24576,4,[2])) #49535^4 (5.1023769743e-16) DrG for p in itertools.count(3): #powers infinite loop emin=1e-9 # min precision of first counter-example at this power print('p=%d...'%p) for a in range(3,10000): for b in range(1,a): c,e=check_fermat(a,b,p) if c and e