There is a task similar to project euler 18, but this time you can only walk over non prime numbers. Task is:
You will have a triangle input below and you need to find the maximum sum of the numbers according to given rules below;
1
8 4
2 6 9
8 5 9 3
1-You will start from the top and move downwards to an adjacent number as in below.
2-You are only allowed to walk downwards and diagonally.
3-You can only walk over NON PRIME NUMBERS.
According to above rules what is the maximum sum of below input? It means please take this pyramid as an input (as file or constants directly inside the code) for your implementation and solve by using it.
215
193 124
117 237 442
218 935 347 235
320 804 522 417 345
229 601 723 835 133 124
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 053 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233
First, tried to solve it without the 3rd condition(only walk over non prime number):
num="""215
193 124
117 237 442
218 935 347 235
320 804 522 417 345
229 601 723 835 133 124
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 053 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233"""
rows=num.split("\n")
array=list()
for row in rows:
num_list=row.split(" ")
array.append(num_list)
for i in range(0,len(array)):
for j in range(0,i+1):
array[i][j] = int(array[i][j])
for i in range(len(array)-1, -1, -1):
for j in range(0,i):
array[i-1][j] += max(array[i][j], array[i][j+1])
print(array[0])
At this point, i dont know how to put the prime number check into this code and find the true solution because above code output is 9022
---edit---
I added the prime number check to print if a number is/is not a prime number:
def isprime(n):
if n < 2: return False
for i in range(2, n):
if n % i == 0:
print("non prime")
break
else:
print("prime")
for i in range(0,len(array)):
for j in range(0,i+1):
print(isprime(array[i][j]))
How should i make it skip the prime numbers and continue with non prime ones?