def possible(y, x, n): ''' initialize variables''' global grid array = [] ''' initialize for loop and if true there is no number similar to the input number inside the row or column of the grid or board.''' for i in range(0, 9): if grid[y][i] == n: if grid[i][x] == n: return False ''' initialize variables and calculate the index location of the board.''' output = var() x0 = (x // 3) * 3 y0 = (y // 3) * 3 ''' initialize a for loop and store the values inside array''' for i in range(0, 3): for j in range(0, 3): array.append(grid[y0 + i][x0 + j]) ''' Applying Kanren functions into the Sudoku algorithm. The functions are use to implement a logic program to verify whether the input variable is identical to the numbers inside the array. If there is no numbers identical to the numbers inside the array, then the function will return True.''' find_number = run(1, output, eq(output, n), membero(output, array)) if len(find_number) != 0: return False return True
def five(): i = int i = 0 temp = var() temp = run(int(a), n, prime_test(n)) while int(temp[i]) < int(a): i = i + 1 i = i - 1 if int(temp[i + 1]) == int(a): return ("{} is a prime number".format(a)) else: j = int k = int j = int(a) - int(temp[i]) k = int(temp[i + 1]) - int(a) if j < k: return ("{} is the closest prime number".format(temp[i])) elif k < j: return ("{} is the closest prime number".format(temp[i + 1])) else: return ("{} and {} are the closest prime numbers".format( temp[i], temp[i + 1]))
def three(): i = int i = 0 factor = [] temp = var() result = False temp = run(int(a), n, prime_test(n)) while i < int(a): if (int(a) == int(temp[i])): result = True i = i + 1 else: i = i + 1 if result == True: return ("{} is a prime number".format(a)) else: i = 0 while int(temp[i]) < int(a): if (int(a) % int(temp[i]) == 0): factor.append(int(temp[i])) i = i + 1 else: i = i + 1 return (factor)
def test_primo(): if not sg: pytest.skip("Test missing required library: sympy.ntheory.generate") x = var() res = (set(run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)), (primo, x)))) assert {23, 29} == res assert ((run(5, x, primo(x)))) == (2, 3, 5, 7, 11)
def test_primo(): if not sg: pytest.skip("Test missing required library: sympy.ntheory.generate") x = var() res = (set( run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)), (primo, x)))) assert {23, 29} == res assert ((run(5, x, primo(x)))) == (2, 3, 5, 7, 11)
def six(): temp = var() temp = run(int(a), n, prime_test(n)) i = int i = 0 z = int z = 0 while int(temp[i]) < int(a): i = i + 1 if int(temp[i]) == int(a): j = int k = int jresult = False kresult = False j = int(temp[i]) + 6 k = int(temp[i]) - 6 temp = run(int(j), n, prime_test(n)) while z < int(j): if (int(j) == int(temp[z])): jresult = True z = z + 1 else: z = z + 1 z = 0 while z < int(k): if (int(k) == int(temp[z])): kresult = True z = z + 1 else: z = z + 1 if jresult == True and kresult == False: return ("{} is a Sexy Prime with {}".format(a, str(j))) elif kresult == True and jresult == False: return ("{} is a Sexy Prime with {}".format(a, str(k))) elif jresult == True and kresult == True: return ("{} is a Sexy Prime with {} and {}".format( a, str(j), str(k))) else: return ("{} does not have a Sexy prime".format(a)) else: return ("{} is not a prime number".format(a))
def two(): i = int i = 0 result = False temp = var() temp = run(int(a), n, prime_test(n)) while i < int(a): if (int(a) == int(temp[i])): result = True i = i + 1 else: i = i + 1 if result == True: return ("{} is prime".format(a)) else: return ("{} is not prime".format(a))
from kanren.core import var, eq, run x = var() output = run(1, x, eq(5, x)) print(output)
def sibling(x, y): temp = var() return conde((parent(temp, x), parent(temp, y)))
def grandparent(x, y): temp = var() return conde((parent(x, temp), parent(temp, y)))
def uncle(x, y): temp = var() return conde((parent(temp, x), grandparent(temp, y)))
def main(stdscr): curses.curs_set(False) FPS = 60 minFrameTime = 1 / FPS frameDelta = 0 height, width = stdscr.getmaxyx() height -= 1 g1Yrange = range(0, int((width / 2) - 1)) g2Yrange = range(int((width / 2) + 1), width - 1) midLoc = int(width / 2 - 1) midWidth = 2 graphWidth = int(width / 2 - 1) g1Title = 'y=sin(x), where x is prime and 0 < x < 100,000' g2Title = 'y=sin(sqrt(x)), where x is prime and 0 < x < 100,000' g1TitleLoc = midLoc - int(graphWidth / 2) - int(len(g1Title) / 2) g2TitleLoc = midLoc + int(graphWidth / 2) - int(len(g2Title) / 2) leftBuffer = [] midBuffer = [] rightBuffer = [] loadingMsg1 = 'Calculating prime numbers between 0 and 100,000' loadingMsg2 = 'Please wait...' stdscr.addstr(int(height / 2), int(midLoc - len(loadingMsg1) / 2), loadingMsg1) stdscr.addstr( int(height / 2) + 1, int(midLoc - len(loadingMsg2) / 2), loadingMsg2) stdscr.refresh() for i in range(0, graphWidth): leftBuffer.append([]) for j in range(0, height): leftBuffer[i].append('1') for i in range(0, midWidth): midBuffer.append([]) for j in range(0, height): midBuffer[i].append(' ') for i in range(0, graphWidth): rightBuffer.append([]) for j in range(0, height): rightBuffer[i].append('1') n = var() primeSet = sorted( set(run(0, n, (membero, n, range(1, 100000)), (prime_test, n)))) for entry in primeSet: frameStart = time.time() g1Col = [] g2Col = [] for i in range(0, height): g1Col.append('1') g2Col.append('1') g1Index = int((math.sin(entry) + 1) / 2 * (height - 1)) g2Index = int((math.sin(math.sqrt(entry)) + 1) / 2 * (height - 1)) g1Col[g1Index] = ' ' g2Col[g2Index] = ' ' del leftBuffer[0] del rightBuffer[0] leftBuffer.append(g1Col) rightBuffer.append(g2Col) stdscr.addstr(0, g1TitleLoc, g1Title, curses.A_UNDERLINE) stdscr.addstr(0, g2TitleLoc, g2Title, curses.A_UNDERLINE) for i in g1Yrange: for j in range(0, height - 1): if leftBuffer[i][j] == ' ': stdscr.addch(j + 1, i, leftBuffer[i][j], curses.A_STANDOUT) else: stdscr.addch(j + 1, i, leftBuffer[i][j]) for i in range(0, midWidth): for j in range(0, height - 1): stdscr.addch(j + 1, midLoc + i, midBuffer[i][j], curses.A_STANDOUT) for i, k in zip(g2Yrange, range(0, graphWidth)): for j in range(0, height - 1): if rightBuffer[k][j] == ' ': stdscr.addch(j + 1, i, rightBuffer[k][j], curses.A_STANDOUT) else: stdscr.addch(j + 1, i, rightBuffer[k][j]) if frameDelta > 0: fpsDisplay = 'FPS: ' + '{0:.2f}'.format(1 / frameDelta) stdscr.addstr(height, 0, fpsDisplay) stdscr.addstr(height, len(fpsDisplay), '\t\tCTRL+C to exit', curses.A_BLINK) frameDelta = time.time() - frameStart while frameDelta < minFrameTime: frameDelta = time.time() - frameStart stdscr.refresh() input()
else: return ("{} is not a prime number".format(a)) def seven(): return ("Thank you for using this program") def choice(argument): switcher = {1: one, 2: two, 3: three, 4: four, 5: five, 6: six, 7: seven} func = switcher.get(argument, lambda: "Invalid choice") print(func()) n = var() x = var() a = int b = int b = 0 a = input( "Please enter the number that you wish to discover some fun prime facts about: " ) print("What would you like to do?") print("Enter 1 for the first {} primes".format(a)) print("Enter 2 to see if {} is prime".format(a)) print("Enter 3 to get the prime factors of {}".format(a)) print("Enter 4 to get all primes from the first {} integers".format(a)) print("Enter 5 to find the closest prime number to {}".format(a)) print("Enter 6 to check if {} has a Sexy Prime counterpart".format(a)) print("Enter 7 to exit")
""" Example using SymPy to construct a prime number goal """ from kanren.core import (isvar, success, fail, assoc, goaleval, var, run, membero, condeseq, eq) from sympy.ntheory.generate import prime, isprime import itertools as it def primo(x): """ x is a prime number """ if isvar(x): return condeseq([(eq, x, p)] for p in it.imap(prime, it.count(1))) else: return success if isprime(x) else fail x = var() print((set(run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)), (primo, x))))) # set([29, 33]) print((run(5, x, primo(x)))) # (2, 3, 5, 7, 11)
from kanren import * from kanren.core import success, fail, goaleval, condeseq, eq, var, lall from sympy.ntheory.generate import prime, isprime import itertools as it import time def prime_check(x): if isvar(x): return condeseq([(eq,x,p)] for p in map(prime, it.count(1))) else: return success if isprime(x) else fail x = var() print((set(run(0,x,(membero,x,(12,14,15,19,20,21,22,23,29,30,41,44,52,62,65,85)),(prime_check,x))))) print((run(10,x,prime_check(x)))) def left(q, p, list): return membero((q,p), zip(list, list[1:])) def next(q, p, list): return conde([left(q, p, list)], [left(p, q, list)]) # Zebra probelm houses = var() rules_zebraproblem = lall( (eq, (var(), var(), var(), var(), var()), houses), (membero,('Englishman', var(), var(), var(), 'red'), houses), (membero,('Swede', var(), var(), 'dog', var()), houses), (membero,('Dane', var(), 'tea', var(), var()), houses),
from kanren.core import var, eq, run x = var() y = var() output = run(1, x, eq((x, y), (y, 3))) print(output)
''' read the json file and store in data ''' # read json with open('profile2.json') as f: data = json.load(f) ''' find the total number of employees ''' total_employees = np.arange(1, len(data), 1) ''' intialize variable n ''' n = var() ''' check the array and filter the numbers that are prime numbers ''' result_id = run(0, n, (membero, n, (total_employees)), (prime_check, n)) ''' the attributes is for the user to read the data ''' attributes = ['index', 'prime', 'first', 'last', 'jobtype'] print('\nHere are all the prime numbers from the database:') ''' print the attributes ''' print('\n' + '\t'.join(attributes)) print('=' * 57) '''
from kanren.facts import Relation, facts, fact from kanren.core import var, run from kanren.goals import membero from kanren import vars ukuran = Relation() warna = Relation() gelap = Relation() facts(ukuran, ("beruang", "besar"), ("gajah", "besar"), ("kucing", "kecil")) facts(warna, ("beruang", "cokelat"), ("kucing", "hitam"), ("gajah", "kelabu")) fact(gelap, "hitam") fact(gelap, "cokelat") z = var() ukuran_h = "kecil" kecil = run(0, z, ukuran(z, ukuran_h)) print("hewan berukuran " + ukuran_h + ": ", kecil)