I would like to write a program for Pythagorean Triplet. Program for numbers a, b, c return Pythagorean three natural numbers a1, b1, c1 such that a1 >= a, b1 >= b, c1 >= c.
def Triplet(a, b, c):
a1 = a
b1 = b
n = 5
m = 0
while True:
m += 1
while b1 <= (b + n * m):
a1 = a
while a1 <= b1:
#while c1 > c:
c1 = (a1 * a1 + b1 * b1) ** .5
if c1 % 1 == 0:
return a1, b1, int(c1)
a1 += 1
b1 += 1
print(Triplet(3,4,6))
For input: (3, 4, 6), output should be: (6, 8, 10). Where is the error?
The issue is that you've commented out your incorrect check for c1 > c, but not replaced it with anything.
If you just add that condition back before the return, it works:
def Triplet(a,b,c):
a1=a
b1=b
n=5
m=0
while True:
m+=1
while b1<=(b+n*m):
a1=a
while a1<=b1:
c1=(a1*a1+b1*b1)**.5
if c1>=c and c1%1==0:
return a1,b1,int(c1)
a1+=1
b1+=1
print(Triplet(3,4,6))
If you change the condition to if c1%1==0 and c1>=c: then the issue will get fixed.
I ran it locally and i got (6, 8, 10)
Related
For function a(0) = 3, a(n) = 2*a(n-1) -1, generator should be like:
def p():
b = 3
while True:
yield b
b = 2 * b -1
So for function c(1) = 9, c(n) = 9*c(n-1) + 10**(n-1)- c(n-1),
how to write the generator for this function?
For the sequence, you have value for c(1) as 9, calculate the value for c(0) using c(1) which turns out to be 1, then write a generator which first yeilds c(0), and c(1), then for each next values, apply the formula, and get the next value and yield it, finally replace the previous value b0 by this next value b1 in order to continue the sequence.
def generate_seq():
b0 = 1
b1 = 9
n=2
yield b0
yield b1
while True:
b1 = 9*b1 + 10**(n-1) - b1
yield b1
b0 = b1
n += 1
seq = generate_seq()
for i in range(10):
print(next(seq))
OUTPUT:
1
9
82
756
7048
66384
631072
6048576
58388608
567108864
Similar to your original, just the extra power of 10:
def p():
c = 1
pow10 = 1
while True:
yield c
c = 8*c + pow10
pow10 *= 10
Try it online!
I'm looking for the best regular expression to match the linear system with 2 unknowns (ax+by=c) for Python module ’re’. Where a, b and c are positive or negative integers and I need to separate the match in
3 groups each one contains the value of a, b and c (with signs): group 1 containing ‘a’ value’s, group 2 containing ‘b’ value’s and group 3 containing ‘c’ value’s.
e.g.:
for -3x+y=-2, group1 will contain -3, group 2 will contain 1 and group 3 will contain -2
e.g.:
x+3y=-4
-2x+y=2
3x-y=2
...
What I used so far is :
r"(^[+-]?\d*)x([+-]?\d*)y=([+-]?\d*)"
It almost woks fine except when it has to deal with a negative sign and a or b are missing.
e.g.:
-x+2y=4
5x-y=3
I have to put 1 before x or y if they're negative to make it work:
-x+2y=4 => -1x+2=4
5x-y=3 => 5x-1y=3
Python code:
import numpy as np
import re
def solve(eq1,eq2):
match1 = re.match(r"(^[+-]?\d*)x([+-]?\d*)y=([+-]?\d*)", eq1)
a1, b1, c1 = match1.groups()
if a1 is None or a1== '':
a1=1
elif a1 == '-':
a1=-1
if b1 is None:
b1=1
elif b1 == '-':
b1=-1
elif b1 == '+':
b1 = 1
a1, b1, c1 = float(a1), float(b1), float(c1)
match2 = re.match(r"([+-]?\d*)x([+-]?\d*)y=([+-]?\d*)", eq2)
a2, b2, c2 = match2.groups()
if a2 is None or a2== '':
a2=1
elif a2 == '-':
a2=-1
if b2 is None:
b2=1
elif b2 == '-':
b2=-1
elif b2 == '+':
b2 = 1
a2, b2, c2 = float(a2), float(b2), float(c2)
A = np.array([[a1, b1], [a2, b2]])
B = np.array([[c1], [c2]])
print(np.linalg.inv(A) # B)
solve("x-y=7","2x+3y=4")
Output:
[[ 5.][-2.]]
Split based on regular expression x|y=, considering empty strings and + or - without numbers.
import re
ee = ['x+3y=-4', '-2x+y=2', '3x-y=2', '-x+2y=4', '5x-y=3']
for e in ee:
print([int(m+'1' if m in ['', '+', '-'] else m)
for m in re.split('x|y=', e)])
Output:
[1, 3, -4]
[-2, 1, 2]
[3, -1, 2]
[-1, 2, 4]
[5, -1, 3]
Update #1:
import numpy as np
import re
def solve(eq1, eq2):
coeffs = []
for e in [eq1, eq2]:
for m in re.split('x|y=', e):
coeffs.append(float(m + '1' if m in '+-' else m))
a1, b1, c1, a2, b2, c2 = coeffs
A = np.array([[a1, b1], [a2, b2]])
B = np.array([[c1], [c2]])
return np.linalg.inv(A) # B
print(solve("x-y=7", "2x+3y=4"))
Output:
[[ 5.]
[-2.]]
Check it online with rextester.
I am a beginner in python and I have used what I know from eclipse to write 4 nested for loops. I would like to condense my loops into a while loop but I am unsure how to do so. Could someone please help me? Here's my code:
import sys
n = int(sys.argv[1])
# step 1 four nested loops
for a in range(1, n + 1):
a3 = a*a*a
if a3 > n:
break
for b in range(a + 1, n + 1):
b3 = b*b*b
if a3 + b3 > n:
break
for c in range(a + 1, n + 1):
c3 = c*c*c
if c3 > a3 + b3:
break
for d in range(c + 1, n + 1):
d3 = d*d*d
if c3 + d3 > a3 + b3:
break
if a3 + b3 == c3 + d3:
print str(a3 + b3), " = ", a, "^3", " + ", b, "^3", " = ", c, "^3", " + ", d, "^3"
I keep getting an EOF error in python 3. Here is my code
num = float(input()) #servings
p = float(input()) #people
a2 = float(input())
b2 = float(input())
c2 = float(input())
d2 = float(input())
e2 = float(input())
f2 = float(input())
g2 = float(input())
h2 = float(input())
i2 = float(input())
a1 = a2 / num
b1 = b2 / num
c1 = c2 / num
d1 = d2 / num
e1 = e2 / num
f1 = f2 / num
g1 = g2 / num
h1 = h2 / num
i1 = i2 / num
a = a1 * p
b = b1 * p
c = c1 * p
d = d1 * p
e = e1 * p
f = f1 * p
g = g1 * p
h = h1 * p
i = i1 * p
lis = str(a)+ str(b)+ str(c)+ str(d)+ str(e)+ str(f)+ str(g)+ str(h)+ str(i)
print (lis) #8 14 1 1 6 2 1 2 .5 2
and the error is on line 11. If I delete line 11 and all code that goes with it, it gives me the error on line 10, then 9, then 8, etc.
The code works fine until you give 11 input values since there are 11 input statements. The EOF error occurs when you don't provide sufficient inputs. I assume the comment on the last line is your input and it has only 10 values. I think that's the reason for the EOF error.
i'm not in computer science or computer related major. i'm civil engineering student, and i'm trying to make app to ease my calculation that can't be calculated by spreadsheet. i know python basic. and please don't be cruel to me haha
so i have this code
from math import ceil
print('Masukan jarak antar pias')
jarakAntarPias = float(input())
print('Masukan kedalaman sondir')
kedalamanSondir = float(input())
jumlahTitik = int(ceil(kedalamanSondir/jarakAntarPias+1))
conus = []
cn2a = []
cn3a = []
for i in range(0, jumlahTitik):
a = float(input())
if a < 0:
b = float(input())
conus[i-1] = [(i-1)*jarakAntarPias, b]
a = float(input())
if a >= 0:
conus.insert(i, [i*jarakAntarPias, a])
conusAtas = []
conusBawah = []
print(conus)
diameter = float(input())
d4 = diameter*4
d8 = diameter*8
y = 0
a1 = 0
while y <= jumlahTitik:
if (d4/jarakAntarPias+1) >= y:
while a1 <= y:
conusAtas.insert(a1, conus[y-a1][1])
a1 += 1
else:
while a1 <= (int((d4/jarakAntarPias))+1):
conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
a1 += 1
a1 = 0
print(conusAtas)
if (d8/jarakAntarPias+1) <= jumlahTitik-y:
while a1 <= jumlahTitik-y:
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
else:
while a1 <= (d8/jarakAntarPias+1):
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
#more code below
print(conusBawah)
conusAtas = []
conusBawah = []
y += 1
my problem is, the code works flawlessly as i expected if i didn't add this code
if (d8/jarakAntarPias+1) <= jumlahTitik-y:
while a1 <= jumlahTitik-y:
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
else:
while a1 <= (d8/jarakAntarPias+1):
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
those code actually similar like this code below
if (d4/jarakAntarPias+1) >= y:
while a1 <= y:
conusAtas.insert(a1, conus[y-a1][1])
a1 += 1
else:
while a1 <= (int((d4/jarakAntarPias))+1):
conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
a1 += 1
but when the program is running this code
conusBawah.insert(a1, conus[y+a1][1])
it always have ListIndex error
this is the traceback
Traceback (most recent call last):
File "C:/Users/afahm/PycharmProjects/untitled2/DULS.py", line 56, in <module>
conusBawah.insert(a1, conus[y+a1][1])
IndexError: list index out of range
please help me, i've been stuck in this code for two days.
thanks
you need to add a condition to all of your while statements where you use conus[y+a1], because y + a1 is bigger that the length of conus.
so... it would look like:
if (d4/jarakAntarPias+1) >= y:
while a1 <= y and (y+a1) < len(conus):
conusAtas.insert(a1, conus[y-a1][1])
a1 += 1
else:
while a1 <= (int((d4/jarakAntarPias))+1) and (int((d4/jarakAntarPias))+1-a1) < len(conus):
conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
a1 += 1
a1 = 0
print(conusAtas)
if (d8/jarakAntarPias+1) <= jumlahTitik-y:
while a1 <= jumlahTitik-y and (y+a1) < len(conus):
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
else:
while a1 <= (d8/jarakAntarPias+1) and (y+a1) < len(conus):
conusBawah.insert(a1, conus[y+a1][1])
a1 += 1
This checks that the length conus is less than y + a1. eg. if conus = [[0.0, 3.0], [1.0, 4.0], [2.0, 5.0]] the len(conus) would be 3. you where getting the IndexError, is because y +a1 was 3, and because conus is Zer0 indexed that would mean thatconus[2] == [2.0, 5.0]. in other wordsconus[3]` would fail.
In this iteration you are filling your list:
for i in range(0, jumlahTitik):
which means, your highest index is jumlahTitik - 1
in this iteration you are trying to access your list:
while y <= jumlahTitik:
On your last iterarion y would be jumlahTitik and a1 would be >0
so conus[y+a1] trys to access conus[jumlahTitik + a1] which cant work, since your highest index is jumlahTitik - 1