not getting output for euler challenge 3 - python

I am attempting to solve Project Euler: Problem 3, and I am using the following function to test for primality,
def check_prime(x):
i = 1
b = 0
while b == 0 :
i += 1
if i >= x :
return True
b += 1
elif x%i == 0 :
return False
b += 1
Which I call from the rest of my program
a = 3
z = []
number_used = 600851475143
while a < number_used/2 :
if check_prime(a) and number_used%a == 0 :
z.append(a);
a += 2
else :
a += 2
print z
But, the code is not printing the prime factors needed for the third Euler problem. Is my code too inefficient to manage it?

Related

Sudoku brute force always returns None [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 1 year ago.
I have reviewed multiple questions regarding similar problems but have not been able to find a solution. I have to write a script for solving and displaying a sudoku puzzle in a text file. If there is no possible solution, it should return None, otherwise it should print the formatted and solved puzzle. My issue is that my solve() function always returns None, even for puzzles with simple solutions. I have tried to debug by following the logic and reviewing my syntax but I fear I am making a simple mistake I cannot see. My functions are
def load_puzzle(path):
with open(path) as fin:
contents = fin.read()
lines = contents.split('\n')
puzzle = []
for i in lines:
token_strings = i.split(' ')
token_ints = [int(x) for x in token_strings]
puzzle.append(token_ints)
return puzzle
def display_puzzle(puzzle):
for i in range(9):
if i == 0 or i == 3 or i == 6:
print('+-------+-------+-------+')
row = '| '
for j in range(9):
if puzzle[i][j] == 0:
row = row + '. '
else:
row = row + str(puzzle[i][j]) + ' '
if j==2 or j==5 or j==8:
row = row + '| '
print(row)
print('+-------+-------+-------+')
def get_next(row, col):
if col < 8:
#print('inside get_next') #DEBUG
return row, col+1
elif col == 8 and row < 8:
return row+1, 0
elif col == 8 and row == 8:
return None, None
def copy_puzzle(puzzle):
new_puzzle = []
for i in puzzle:
new_puzzle.append(i.copy())
return new_puzzle
def get_options(puzzle, row, col):
if puzzle[row][col] > 0:
return None
used = []
for i in puzzle[row]:
if i > 0:
used.append(i)
for i in range(9):
if puzzle[i][col] > 0:
used.append(puzzle[i][col])
start_row = 3*int(row/3)
start_col = 3*int(col/3)
for i in range(2):
for j in range(2):
if puzzle[start_row + i][start_col +j] > 0:
used.append(puzzle[start_row + i][start_col +j])
options = []
for i in range(1, 10):
if i not in used:
options.append(i)
return options
def solve(puzzle, row=0, col=0):
if puzzle[row][col] != 0:
#print("here") # debuggin
next_row, next_col = get_next(row, col)
#print(next_row, next_col) # debugging
if next_row is None:
return puzzle
else:
solve(puzzle, next_row, next_col)
if puzzle[row][col] == 0:
# print("there") # debuggin
options = get_options(puzzle, row, col)
#print(options) #debuggin
if options == []:
return None
for i in options:
new_puzzle = copy_puzzle(puzzle)
new_puzzle[row][col] = i
# display_puzzle(new_puzzle) #debuggin
result = solve(new_puzzle, row, col)
if result is not None:
return result
The commented out print() functions are ones I used to follow the loops to make sure the functions were operating as intended. As far as I can tell they were, but with so many loops, Jupyter Notebook began to print over itself and the display became indecipherable, as well as the making the function have an unreasonable resolution time.
The initial test puzzle is a .txt file containing:
5 0 0 0 0 0 0 0 0
0 9 0 7 0 0 8 0 0
0 0 0 0 3 0 0 7 0
6 0 1 0 0 0 9 8 0
0 0 0 6 0 0 0 0 0
0 0 9 0 0 0 7 0 1
0 0 0 0 0 8 1 9 0
0 4 0 5 0 1 0 0 8
0 7 0 3 0 6 0 4 0
Solved:
Missing return statement before solve(puzzle, next_row, next_col).
also ranges for cell check in get_options() should be set to 3 instead of 2.

Find the sum of all the multiples of 3 or 5 below 1000. (idk what is wrong with my code)

I'm a beginner and I tried this code to list the sum of all the multiples of 3 or 5 below 100, but it gives a wrong answer and I don't know why.
result = 0
result2 = 0
som = 0
sum2 = 0
below_1000 = True
while below_1000:
result = result+3
if result < 1000:
som += result
else:
below_1000 = False
below_1000 = True
while below_1000:
result2 = result2+5
if result2 < 1000:
sum2 += result2
else:
below_1000 = False
final_result = som+sum2
print(final_result)
Since you first loop over multiples of 3, then again over multiples of 5, you are double-counting a lot of values, specifically values that are multiples of both 3 and 5 (for example 15 or 60).
To write this manually, you can use a for loop over range
total = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
total += i
>>> total
233168
A more concise way to do this same thing is using a generator expression within the sum function
>>> sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0)
233168

Why do I get 0 when this python code is executed?

The python code, given in a lab for a class I'm in when executed through pycharm, ouputs 0. However, upon looking at the code, it should be 1. Why is it 0?
X = 4
C = 0
while X > 0:
if X % 2 == 0:
C = C + 1
else:
C = C - 1
X = X - 1
print(C)
The code is fine. Your X will go from 4 to 1 and on X = 0, the program will leave the loop. Printing X-C for each iteration gives the values:
X-C
4-1
3-0
2-1
1-0
If you want it to go till 0, make the condition as:
while X >= 0:
The loop executes 4 times: When X = 4, C = 1; when X = 3, C = 0; when X = 2, C = 1; when X = 1, C = 0.

Binsary Search (BSEARCH1)

I was going through this problem on SPOJ.
It is a problem about binary search.
I tried to implement this in python:
x,yu= input().split()
bu=int(yu)
y=int(yu)
array=input().split()
while y>0:
qquery=input()
y=y-1
query=int(qquery)
b= int(x)
left=1
right= b
while left <= right and bu>0:
pmid=((right+left)/2-1)
mid=int(pmid)
fir=array[mid]
fire=int(fir)
if fire== query:
bu=bu-1
if query < fire:
left=mid+1
else :
right=mid-1
this is the input:
5 4
2 4 7 7 9
7
10
4
2
I am getting an infinite loop with 3.
I have been stuck on this problem for a long time. I would really like someone to point out my mistake, the solution and the explanation.
Thank you!!
Try this code if you're looking for the binary search algorithm.
def binary_search(seq, t):
min = 0
max = len(seq) - 1
while True:
if max < min:
return -1
m = (min + max) // 2
if seq[m] < t:
min = m + 1
elif seq[m] > t:
max = m - 1
else:
return m
seq = [1, 2, 3, 4]
t = 2
binary_search(seq, t)

Loop not functioning correctly

Working with data frames and this is the code I have for it.
numbers = 3
count=0
A = 0
B = 0
C = 0
for x in range(numbers):
if str(data.iloc[count])== 'A':
A += 1
elif str(data.iloc[count])== 'B':
B += 1
elif str(data.iloc[count])== 'C':
C += 1
count +=1
#this is to return the count to check if it works
print A
print B
print C
but for some reason when I run this code only the count for A increases.
i.e. if the data in the index had a 'A', 'B', 'B' its still returning A = 3 and B = 0 where it should be returning A = 1, B = 2, and C = 0
what am I doing wrong? thanks again.
Since your count += 1 is not within the for loop, count += 1 only runs once, after the for loop is complete. It needs to be indented. Alternatively, you do not need to use a count variable since x is already going through the range 0 to 3:
numbers = 3
A = 0
B = 0
C = 0
for x in range(numbers):
if str(data.iloc[x])== 'A':
A += 1
elif str(data.iloc[x])== 'B':
B += 1
elif str(data.iloc[x])== 'C':
C += 1
#this is to return the count to check if it works
print A
print B
print C
This also worked
count=0
numbers = 3
A = 0
B = 0
C = 0
for x in range(numbers):
count +=1
if str(data.iloc[x])== 'A':
A += 1
elif str(data.iloc[x])== 'B':
B += 1
elif str(data.iloc[x])== 'C':
C += 1
#this is to return the count to check if it works
print A
print B
print C

Categories