generating Pythagorean quadruples - pythagorean

How do you generate a list of Pythagorean quadruples
def quadruple_py(num):
quad_list = []
for n in range(1, num):
for m in range(n+1, num+1):
for p in range(m+1, num+2):
for q in range(p+1, num+3):
if n2 + m2 + p2 + q2> num:
break
if n % 2 == 1 and m % 2 == 1 and p % 2 == 1 and q % 2 == 1:
continue
if mygcd(n, m) != 1:
continue
w,e,r,t = mergesort([m2+n2-p2-q2, 2(mq+np), 2(nq-mp), n2+m2+p2+q2])
quad_list.append((w,e,r,t))
return quad_list

Related

Why does my code stop after the first test case? (Problem -- https://www.hackerearth.com/practice/codemonk/)

Please review my code for the problem given in the title, it stops after showing correct output for the first test case and shows Runtime error if I put t > 1 in Hackerearth IDE.
t = int(input())
while t > 0:
t = t - 1
n, k = map(int, input().split())
A = [int(i) for i in input().split()]
p = []
if k > n:
k = k - n
elif k == n:
k = 0
else:
k = k
for j in range(k):
x = A.pop()
p.append(x)
p = p[::-1]
for j in range(len(A)):
p.append(A[j])
print(*p)

solve n which is an integer above 0 in python

This code works but it is not very efficient is there any help on a faster code in python to find n knowing that n is an integer above 0 and that n has no upper bound, how(x) will return you 1 if x>n, 0 if x = n, and -1 if x
def how(x):
if x > n:
return 1
elif x < n:
return -1
else:
return 0
def find(how):
if how(1) == 1:
return 1
x = 2
while how(x) != 1:
x = x**x
v = x
while how(x) != 0:
if how(x) == 1:
v = x
x = (x+1)//2
else:
x += (v-x+1)//2
return x
Rebecca, I've added some print statements so you can see where goes what wrong. As Patrick Artner said... its a bit confusing which way to go so I've tried to clean-up some things that enable you to continue exploring comparison of two variables against each other (and fake error catching (0).
Lets start and remove the confusing lingo and produce something workable code. With current below script it runs and with value = 1, reference = 1 you get the below print result in a continues loop until YOU stop the script manually:
v1 = n: error
loop1 1
def selector(v1, n):
if v1 > n:
print 'v1 > n', v1, n
return 1
elif v1 < n:
print 'v1 < n', v1, n
return -1
else:
print 'v1 == n: error'
return 0
def find(value, reference):
if selector(value, reference) == 1:
return 1
while selector(value, reference) != 1:
x = value**value
print 'loop1', x
v = x
while selector(value, reference) != 0:
print 'loop2'
if selector(value, reference) == 1:
v = value
x = (value+1)/2
print 'loop2-if', v, x
else:
x += (v-(value+1))/2
print 'loop2-else', x
print ' Almost done...'
return x
if __name__ == '__main__':
n = 1
print find(1, 1)
Happy exploring,....

List Index out of range, trying to get the [-1] position

I've tested to see if the list is empty and I'm sure it's not empty. So why this error continues?
The code is to find the smallest amount of factorial to a sum is equal to N.
def fat(n):
if n == 0 or n == 1:
return 1
else:
return n * fat(n - 1)
n = int(input())
ns = [x+1 for x in range(n)]
listaFat = []
l = []
for x in ns:
if fat(x) < n:
listaFat.append(x)
maior = max(listaFat)
listaFat.remove(maior)
print(listaFat)
print(len(listaFat))
while (fat(listaFat[-1]) + fat(maior)) < n:
l.append(listaFat[-1])
if len(listaFat) > 0:
listaFat.remove(listaFat[-1])
continue
else: break
print(l)
print(listaFat)

Puzzler solver program: How many different solutions are there to (1/a)+(1/b)+(1/c)+(1/d)+(1/e)+(1/f)+(1/g) = 1?

I wrote the python code below that solves and prints each possible solution for anything under 6 unit fractions, but given how I programmed it, it takes infinitely long to check for 7 fractions. Any ideas on how to modify the code to find all the possible solutions more efficienty?
import sys
from fractions import Fraction
import os
#myfile = open('7fractions.txt', 'w')
max = 7 #>2 #THIS VARIABLE DECIDES HOW MANY FRACTIONS ARE ALLOWED
A = [0] * max
A[0] = 1
def printList(A):
return str(A).strip('[]')
def sumList(A):
sum = 0
for i in A:
if i != 0:
sum += Fraction(1, i)
return sum
def sumTest(A):
sum = 0
v = 0
for i in range(0, len(A)):
if A[i] == 0 and v == 0:
v = Fraction(1,A[i-1])
if v != 0:
sum += v
else:
sum += Fraction(1, A[i])
return sum
def solve(n, A):
if n == max - 2:
while (sumTest(A) > 1):
print(A)
if sumList(A) < 1:
e = 1 - sumList(A)
if e.numerator == 1 and e.denominator>A[n-1]:
A[n+1] = e.denominator
#myfile.write(printList(A) + '\n')
print(A)
A[n+1] = 0
A[n] += 1
else:
while (sumTest(A) > 1):
if sumList(A) < 1:
A[n+1] = A[n] + 1
solve(n+1, A)
A[n+1] = 0
A[n] += 1
#execute
solve(0, A)

Nth Fibonacci number

I am failed to print only the nth fibonacci number.
In my code, when the user said to print nth trem it print the series upto nth term but i want to get the output only the nth term
e.g
if I say num=4
out put should be 2
please guide
here is the code:
N= int(input("How many terms? "))
N1 = 0
N2 = 1
sum = 2
if N <= 0:
print("Plese enter a positive integer")
elif N == 1:
print("Fibonacci sequence:")
print(N1)
else:
print("Fibonacci sequence:")
print(N1,",",N2,end=' , ')
while sum < N:
Nth = N1 + N2
print(Nth,end=' , ')
N1 = N2
N2 = Nth
sum += 1
The print stmt should be outside the loop
N= int(input("How many terms? "))
N1 = 0
N2 = 1
sum = 2
if N <= 0:
print("Plese enter a positive integer")
elif N == 1:
print("Fibonacci sequence:")
print(N1)
else:
print("Fibonacci sequence:")
print(N1,",",N2,end=' , ')
while sum < N:
Nth = N1 + N2
N1 = N2
N2 = Nth
sum += 1
print(Nth,end=' , ')
Simpler code, from the "How to Think Like a Comptuer Scientist: Python" book,
def fibonacci (n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
Just call fibonacci passing your nth term as the argument.
to achieve that output you can simply decrease the value of n by 1 and then carry on all the computation.
For example:
def fib(n):
n = n-1
a, b = 0, 1
count = 1
while count <= abs(n):
next = a + b
a = b
b = next
count += 1
return a

Resources