|
@@ -0,0 +1,212 @@
|
|
|
+
|
|
|
+
|
|
|
+"""
|
|
|
+Goldbach - 2017 - by psy (epsylon@riseup.net)
|
|
|
+
|
|
|
+You should have received a copy of the GNU General Public License along
|
|
|
+with Goldbach; if not, write to the Free Software Foundation, Inc., 51
|
|
|
+Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
+"""
|
|
|
+import os, sys
|
|
|
+import matplotlib.pyplot as plt
|
|
|
+
|
|
|
+class Goldbach(object):
|
|
|
+ def __init__(self):
|
|
|
+ self.l="love"
|
|
|
+ self.dimX=[]
|
|
|
+ self.dimY=[]
|
|
|
+
|
|
|
+ def banner(self):
|
|
|
+ print " ____ _ _ _ _ "
|
|
|
+ print " / ___| ___ | | __| | |__ __ _ ___| |__ "
|
|
|
+ print "| | _ / _ \| |/ _` | '_ \ / _` |/ __| '_ \ "
|
|
|
+ print "| |_| | (_) | | (_| | |_) | (_| | (__| | | |"
|
|
|
+ print " \____|\___/|_|\__,_|_.__/ \__,_|\___|_| |_|"
|
|
|
+ print "\n", 75*"="
|
|
|
+ print " 'Every even integer greater than 2 can"
|
|
|
+ print " be expressed as the sum of two odd primes'"
|
|
|
+ print 75*"=","\n"
|
|
|
+
|
|
|
+ def is_prime(self, n):
|
|
|
+ for i in range(3, n):
|
|
|
+ if n % i == 0:
|
|
|
+ return False
|
|
|
+ return True
|
|
|
+
|
|
|
+ def is_odd(self, n):
|
|
|
+ if int(n) & 1:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+ def is_valid_root(self, r):
|
|
|
+ import random
|
|
|
+ while True:
|
|
|
+ n=(random.randint(4,100))
|
|
|
+ r=self.is_odd(n)
|
|
|
+ if r is False:
|
|
|
+ break
|
|
|
+ return n
|
|
|
+
|
|
|
+ def threads(self, n):
|
|
|
+ while True:
|
|
|
+ n=n-1
|
|
|
+ ion = self.is_odd(n)
|
|
|
+ if ion is True:
|
|
|
+ ipn = self.is_prime(n)
|
|
|
+ if ipn is True:
|
|
|
+ self.dimX.append(int(n))
|
|
|
+ if n == 1:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ pass
|
|
|
+
|
|
|
+ def generate_graph(self):
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n[Info] Generating 'plots' for number:", self.root
|
|
|
+ if not os.path.exists("graphs/"):
|
|
|
+ os.mkdir("graphs/")
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n + Thread(s):", len(self.tree)
|
|
|
+ print ' - Tree =', self.tree
|
|
|
+ plt.figure()
|
|
|
+ fig = plt.figure(1)
|
|
|
+ ax = fig.add_subplot(111, facecolor='black')
|
|
|
+ for t in self.tree:
|
|
|
+ x = t.rsplit('=',1)[1]
|
|
|
+ x = x.rsplit('+',1)[0]
|
|
|
+ y = t.rsplit('+',1)[1]
|
|
|
+ ax.scatter(self.tree.index(t)+1, x, color="red", s=2)
|
|
|
+ ax.scatter(self.tree.index(t)+1, y, color="green", s=2)
|
|
|
+ header = '"Tree" for number '+str(self.root)
|
|
|
+ plt.title(header)
|
|
|
+ plt.ylabel('Number(s)')
|
|
|
+ plt.xlabel('Thread(s)')
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ plt.show()
|
|
|
+ g = "graphs/"+self.root
|
|
|
+ if not os.path.exists(g):
|
|
|
+ os.mkdir(g)
|
|
|
+ f = open(g+"/"+self.root+"-Goldbach_tree.txt", 'wb')
|
|
|
+ fig.savefig(g+"/"+self.root+"-Goldbach_graph.png")
|
|
|
+ for t in self.tree:
|
|
|
+ f.write(str(t+"\n"))
|
|
|
+ f.close
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n[Info] Generated 'tree' secuence at folder: "+g+"/\n"
|
|
|
+ else:
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n[Info] You have this 'tree' secuence previously saved...\n"
|
|
|
+ ax.clear()
|
|
|
+ print 75*"=", "\n"
|
|
|
+
|
|
|
+ def generate_forest(self, rng):
|
|
|
+ print "\n[Info] Generating 'forest' until range:", rng, "\n"
|
|
|
+ rng=int(rng)
|
|
|
+ for i in range(rng):
|
|
|
+ i=i+1
|
|
|
+ if i > 2:
|
|
|
+ t = self.is_odd(i)
|
|
|
+ if t is False:
|
|
|
+ n = i
|
|
|
+ self.root = str(n)
|
|
|
+ print "[Info] Generating 'tree' for number:", self.root
|
|
|
+ self.generate_tree(n)
|
|
|
+
|
|
|
+ def generate_tree(self, n):
|
|
|
+ s=0
|
|
|
+ t=0
|
|
|
+ x=0
|
|
|
+ y=0
|
|
|
+ w=False
|
|
|
+ self.tree = []
|
|
|
+ try:
|
|
|
+ if int(n) & 1:
|
|
|
+ print "\n[Error] Number should be always an even. Aborting...\n"
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ if int(n) <= 2:
|
|
|
+ print "\n[Error] Number should be always an integer > 2. Aborting...\n"
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ self.threads(n)
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n[Info] Prime numbers detected on serie:", len(self.dimX)
|
|
|
+ self.dimY = self.dimX
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print "\n[Info] Calculating all possible permutations...\n"
|
|
|
+ for p in self.dimX:
|
|
|
+ for d in self.dimY:
|
|
|
+ t = t + 1
|
|
|
+ r = p + d
|
|
|
+ if r == n:
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print " Thread:" +str(t) , "->", p, "+", d, "=", r, "-> FOUND!"
|
|
|
+ l = str(r)+ " = " + str(p)+" + "+str(d)
|
|
|
+ if l not in self.tree:
|
|
|
+ self.tree.append(l)
|
|
|
+ else:
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ print " Thread:" + str(t), "->", p, "+", d, "=", r, "-> DISCARDED..."
|
|
|
+ except:
|
|
|
+ print "\n[Error] Number should be always an integer > 2. Aborting...\n"
|
|
|
+ return
|
|
|
+ print "\n[Info] Combinations found:", str(len(self.tree)), "\n"
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ for t in self.tree:
|
|
|
+ print " ", t
|
|
|
+ graph=raw_input("\nWanna generate a 'graph'? (Y/n)")
|
|
|
+ if graph == "n" or graph == "N":
|
|
|
+ print ""
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ self.generate_graph()
|
|
|
+ else:
|
|
|
+ self.generate_graph()
|
|
|
+
|
|
|
+ def run(self, opts=None):
|
|
|
+ self.banner()
|
|
|
+ self.mode=raw_input("Set mode: single random (default), manual, learning (S/m/l): ")
|
|
|
+ print 40*"-"
|
|
|
+ if self.mode == "m" or self.mode == "M" or self.mode == "Manual" or self.mode == "manual":
|
|
|
+ n=raw_input("Set a number: ")
|
|
|
+ try:
|
|
|
+ n = int(n)
|
|
|
+ except:
|
|
|
+ print "\n[Error] Number should be always an even INTEGER greater than 2. Aborting...\n"
|
|
|
+ sys.exit(2)
|
|
|
+ elif self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ rng=raw_input("Set max range (ex: 100 or 9000000) (PRESS ENTER = 100) (STOP = CTRL+z): ")
|
|
|
+ if not rng:
|
|
|
+ rng=100
|
|
|
+ else:
|
|
|
+ r=raw_input("Set max range (ex: 100 or 9000000) (PRESS ENTER = 100): ")
|
|
|
+ if not r:
|
|
|
+ r=100
|
|
|
+ n = int(self.is_valid_root(r))
|
|
|
+ if not self.mode == "l" or self.mode == "L" or self.mode == "Learn" or self.mode == "learn":
|
|
|
+ self.root = str(n)
|
|
|
+ print "[Info] Generating 'tree' for number:", self.root
|
|
|
+ self.generate_tree(n)
|
|
|
+ else:
|
|
|
+ try:
|
|
|
+ rng = int(rng)
|
|
|
+ except:
|
|
|
+ print "\n[Error] Max range should be always an even INTEGER greater than 2. Aborting...\n"
|
|
|
+ sys.exit(2)
|
|
|
+ if not int(rng) > 2:
|
|
|
+ print "\n[Error] Max range should be always an even integer GREATER THAN 2. Aborting...\n"
|
|
|
+ sys.exit(2)
|
|
|
+ else:
|
|
|
+ t = self.is_odd(rng)
|
|
|
+ if t is True:
|
|
|
+ print "\n[Error] Max range should be always an EVEN integer greater than 2. Aborting...\n"
|
|
|
+ sys.exit(2)
|
|
|
+ else:
|
|
|
+ self.generate_forest(rng)
|
|
|
+ print "[Info] 'Forest' correctly generated. Exiting...\n"
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ app = Goldbach()
|
|
|
+ app.run()
|