@op/DNN 3-1
Python3

No description

Files
  • main.py
main.py
import numpy as np
from random import randrange

z = []
a = []
lw = 0

def act(lst):
  return ((1+np.exp(-(lst)))**-1)

def actp(lst):
  return ((np.exp(lst)))*((np.exp(lst)+1)**-2)

def W(n,m):
  return 4*np.random.normal(size=(n,m))-2

def wts(hl):
  lw = len(hl)-1
  w = []
  for i in range(lw):
    w.append(W(hl[i],hl[i+1]))
  return w

def fw(I,w):
  lw=len(w)
  Ohat = I
  a.append(I)
  for i in range(lw):
    Ohat = np.dot(Ohat,w[i])
    z.append(actp(Ohat))
    Ohat = act(Ohat)
    a.append(Ohat)
  return Ohat

def bw(I,w,cost):
  lw = len(w)
  d = list(range(lw))
  d[-1]= np.multiply(cost,z[-1])
  for i in range(lw-1):
    d[-i-2]= np.dot(d[-i-1],w[-i-1].transpose())*z[-i-2]
  for i in range(lw):
    d[-i-1]= np.dot(a[-i-2].transpose(),d[-i-1])
  return d, w

def wc(dwwI):
  for i in range(len(dwwI[1])):
    dwwI[1][i]=dwwI[1][i]+dwwI[0][i]*25

def HL(li,n,lo,hlw):
  HiLa = [hlw]*(n+2)
  HiLa[0]  = li
  HiLa[-1] = lo
  return HiLa[:]

##############################################################

def check(a, s, d, n):
	x = pow(a, d, n)
	if x == 1:
		return True
	for i in range(s - 1):
		if x == n - 1:
			return True
		x = pow(x, 2, n)
	return x == n - 1

def miller_rabin1(n, k=20):
	if not n & 1:
		return 0
	if n < 6:
	  return 0
	s = 0
	d = n - 1
	while d % 2 == 0:
		d >>= 1
		s += 1
	for i in range(k):
		a = randrange(2, n-2)
		if not check(a, s, d, n):
			return 0
	return 1

###########################################################

def func(fd,i):
  fd = np.array(fd)
  fd = np.round_(fd*10-np.array([3,3,3,-1,-2])).tolist()
  return fd[0]*i*i+ fd[1]*i+ fd[2]+ fd[3]*fd[4]**i

def func2(fd,i):
  fd = np.array(fd)
  fd = np.round_(fd*10-np.array([3,3,3,-1,-2])).tolist()
  return fd[0]*i*i+ fd[1]*i+ fd[2]+ fd[3]*np.exp(fd[4]*i)

###########################################################

def seq_check(fd):
  k = 0
  for i in range(1,20):
    c = func(fd,i)
    if c < 10**100:
      k += miller_rabin1(int(c))
  return k

def seq_dis(fd):
  for i in range(1,100):
    f = int(func(fd,i))
    if f < 10**100:
      print(f,miller_rabin1(f))
  print()

##############################################################

def grad_adj(lis,nudge):
  #return lis
  return np.where(lis==np.amax(lis),nudge, 0)

def grad(fd, nudge=0.5):
  grad = np.zeros((1,len(fd[0])))
  for i in range(len(fd[0])):
    fd[0][i] = fd[0][i]+nudge
    bt = seq_check(fd[0])
    fd[0][i] = fd[0][i]-2*nudge
    grad[0][i] = 10*(bt-seq_check(fd[0]))
    fd[0][i] = fd[0][i]+nudge
  return grad_adj(grad[:],nudge)


def nn(I,nudge=0.75,iterations=500, hls=2, hlw=3):
  hl = HL(len(I[0]),hls,5,hlw)
  w = wts(hl)
  
  while seq_check(fw(I,w)[0]) < 3:
    w = wts(hl)
  rev_check = seq_check(fw(I,w)[0])
  
  fd = fw(I,w)
  for i in range(iterations):
    fd = fw(I,w)
    w1 = w[:]
    wc(bw(I,w,grad(fd,nudge)))
    
    rev_check1 = seq_check(fd[0])
    if rev_check1 < rev_check:
      w = w1
      nudge = 0.1*np.random.randn()+nudge
    rev_check = rev_check1
    
  return fd,seq_check(fd[0])


In = np.array([[0.5,1]])

arrl = []

for i in range(10000):
  n = nn(In,iterations=60)
  arr = np.round_(n[0]*10-np.array([3,3,3,-1,-2]))[0]
  arr_dup_check = 10000*arr[0]+1000*arr[1]+100*arr[2]+10*arr[3]+arr[4]
  if n[1] > 7 and not (arr_dup_check in arrl):
    arrl.append(arr_dup_check)
    print(str(int(arr[0]))+"n² + "+str(int(arr[1]))+"n + "+str(int(arr[2]))+" + "+str(int(arr[3]))+" × "+str(int(arr[4]))+"^n")
    print(n[1],"prime numbers found \n")










Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux