I am practising an old assignment that I never managed to understand. I understand and get the correct output until '3, 11'. After that comes a = f2(b, c). What I don't understand is, f2 is normally defined as f2(c,a). I tried changing all c to b and all a to c in f2, but when I get to the step b = f1(c) (which is now b = f1(b), and then replace all a in f1 with b, I don't get the correct output with show(a, b) in the f1 function. The output should be a = 10 and b = 6, but since I've replaced all the a's, there is not an a left. I have no clue what I should be doing at the a = f2(b, c) step. Could someone please explain the further steps in order to reach the last 3 correct outputs?
The assignment: (sorry for the bad spacing, I can't manage to paste it properly, it pastes as one whole long line)
a = 3
b = 4
def show (x, y):
print ā%d, %dā % (x, y)
def f1 (a):
global b
a *= 2
b += 1
c = a + b
show(a, b)
return c
def f2 (c, a):
a += 3
b = f1(c)
show(c, b)
c = a + b
return c
show(a, b)
c = f1(a)
show(a, c)
a = f2(b, c)
show(a, b)
What will be printed when this program is executed?
The desired output:
3, 4
6, 5
3, 11
10, 6
5, 16
30, 6
Related
As part of a raytracer experiment I'm working on in my high school classes, I need to make it so that I can get the 4 parts of a plane equation from 3 different points. By 4 parts i mean in the equation Ax + By + Cz = D I need to find A, B, C, and D. I understand the math behind this as its relatively simple vector math, but my code doesn't seem to work.
The function I use to construct the Plane object from the 3 points is as follows:
Plane::Plane(Vec3 A, Vec3 B, Vec3 C)
{
//Getting both vectors
Vec3 AB = B - A;
Vec3 AC = C - A;
//Cross Product
Vec3 cr = AB.cross(AC);
a = cr.getX();
b = cr.getY();
c = cr.getZ();
d = a * A.getX() + b * B.getY() + c * C.getZ();
}
In this, Vec3 is just a vector class that holds (x, y, z), and the function names are pretty self explanatory (I hope).
An example of what it outputs:
If I put the vectors (-3, 0, 1), (2, 3, 0), and (0, 2, 3) into this, I get the following results
A = 8
B = -13
C = 1
D = -60
A, B, and C in this are correct, but D is not.
I'm not entirely certain what's wrong with the code, since it will sometimes get the output correctly on certain vectors, sometimes get parts correct, or sometimes get nothing correct at all, which leads me to believe there's a math mistake. Any help is appreciated.
Since in your example, you get the values for A, B, and C correct, the first place to look is in the calculation of D.
In your calculation of d, you use parts of three different vectors. This is not what the equation for D says to do. You want to use the three parts from one vector.
d = a * A.getX() + b * A.getY() + c * A.getZ();
This should work for any of the three vectors.
Operating System : Windows 10
Code I wrote :
program wire
implicit none
INTEGER :: A, B, C, Y, N, X
A = 2
B = 1
C = 3
Y = 5
N = 4
X = 2
if (A ==X) then
Read * , Y
end if
end program wire
When I run this program, how can I modify the code to produce a blank value when a blank appears?
I am not sure if I understand your question correctly, but is this not working?
program wire
implicit none
INTEGER :: A, B, C, Y, N, X
A = 2
B = 1
C = 3
Y = 5
N = 4
X = 2
if (A == X) then
print * , Y
end if
end program wire
We have two integer arrays, a and b, and an integer target value v. Determine whether there is a pair of numbers, where one number is taken from a and the other from b, that can be added together to get a sum of v. Return true if such a pair exists, otherwise return false.
For example:
For a = [1, 2, 3], b = [10, 20, 30, 40], and v = 42, the output should be
sumOfTwo(a, b, v) = True
My code so far:
def sumOfTwo(a, b, v):
for x in a:
for y in b:
if x+y == v:
return True
return False
I want to reduce the execution time, as its taking long to execute long lists.
It should be much faster if you first convert b to a set:
def sumOfTwo(a, b, v):
b = set(b)
return any(v - x in b for x in a)
The complexity should be O(M + N) in stead of O(MN) for the brute force solution.
I have to write a program that solves the 7-11 problem (if you don't know what this is, Google will explain) but instead of finding the values that add and multiply to make $7.11, I have to find all the unique values of a, b, c and d that add and multiply to make $x.yz between $1.00 and $9.99.
It gets to the if statement where it checks if a, b, c and d multiply to make n but it is never true.
def factors(n):
#Finds all the factors of n and adds them to an array
factors_of_n = []
for i in range(1, n):
if(n % i == 0):
factors_of_n.append(i)
#Runs through the array and checks if they add and multiply to
#equal n
for a in factors_of_n:
for b in factors_of_n:
for c in factors_of_n:
for d in factors_of_n:
if(a < b and b < c and c < d):
if(a + b + c + d == n):
if(a * b * c * d == n * 1000000):
#It never gets into this loop
print "please"
return True
def g711():
min = 100
max = 999
count = 0
for n in range(min, max):
if factors(n):
print "yay"
I just need somebody that is a bit better at maths than I am to check it over and see where I am going wrong.
This can be simplified using itertools.combinations_with_replacement:
from itertools import combinations_with_replacement as cwr
def factors(n):
...
r = {}
for n in range(100, 999):
for a, b, c, d in cwr([f for f in factors(n*1000000) if f < n], r=4):
if a+b+c+d == n and a*b*c*d == n*1000000:
r.setdefault(n, []).append((a,b,c,d))
Results:
{644: [(125, 160, 175, 184)],
651: [(125, 140, 186, 200)],
660: [(110, 150, 200, 200)],
...
711: [(120, 125, 150, 316)],
...
992: [(32, 250, 310, 400)]
}
I am a Newbie to Fortran, facing a problem within a Do loop. I am programming a Fortran Code for a MEX File to be used within Matlab.
I assume it has a problem with the definition of k and z, but I don't see why. Maybe you guys have a hint for me what I am doing wrong. Thank you very much!
Error Message and Code
innerloops.F
do k = 1, 4
1
Error: Non-numeric character in statement label at (1)
innerloops.F
do k = 1, 4
1
Error: Unclassifiable statement at (1)
innerloops.F
do z = 1, 25
1
Error: Non-numeric character in statement label at (1)
innerloops.F
do z = 1, 25
Error: Unclassifiable statement at (1)
C Computational routine
subroutine innerloops(J,c1,c2,c3,c4,n1,n2,n3,n4,y,m,n)
mwSize m, n
integer k, z
real*8 J(m,n), y(4,1), c1, c2, c3, c4, n1, n2, n3, n4
real*8 QuadRuleX(25,2)
real*8 QuadRuleW(25,1)
real*8 X(5,1), r, t
real*8 P, c_h, n_h
integer h = 10
C Gaussian Points
X(1) = -.906179
X(2) = -.538469
X(3) = 0
X(4) = .538469
X(5) = .906179
C Corresponding QuadRule points
QuadRuleX(1,1) = X(1)
QuadRuleX(1,2) = X(1)
C .... (snipped it here for readability)
C Corresponding weights
QuadRuleW(1) = Y(1)*Y(1)
QuadRuleW(2) = Y(2)*Y(1)
C .... (snipped it here for readability)
do k = 1, 4
do z = 1, 25
r = QuadRuleX(z,1)
t = QuadRuleX(z,2)
P = shape(k,r,t)
c_h = c1*shape(k,r,t)
n_h = n1*shape(k,r,t)
y(k,1) = (P*((((h-1)*c_h)/(h-1)*c_h+1))*n_h*(2-n_h)-n_h)
continue
continue
return
end do
end subroutine innerloops
C defining the shape functions
Function shape(q,c,d)
implicit none
real q,c,d,P
if (q == 1) then
P = 1/4*(c-1)*(d-1)
else if (q == 2) then
P = -1/4*(c+1)*(d-1)
else if (q == 3) then
P = 1/4*(c+1)*(d+1)
else if (q == 4) then
P = -1/4*(c-1)*(d+1)
endif
return
end Function shape
By using a .F suffix the compiler by default assumes that you are using a fixed format source code. In fixed format certain columns are reserved for special purposes. Here it appears that your "do" has been mistakenly put into a column reserved for statement label (columns 1 through 5). Your statement has to fit between column 7 and 72 in a fixed-format fortran file. This is what the compiler was complaining about. As mentioned by others, your code also contain other errors that need to be fixed.
To make things simpler, you can use a free format instead by changing the suffix to .f90 and replacing the "C" comment indicator with "!".