Pari/GP Exceptions/break loops - exception

ellpow(E, P, m) will always throw an exception:
*** ellpow: impossible inverse modulo: Mod(x, y).
*** Break loop: type 'break' to go back to GP
where x and y are integers.
I want to trap the value x, without finishing the program in order to use it
later.
The snippet of the code is:
trap(invmoder,
x,
ellpow(E, P, m)
), n);
The whole program is:
ellcurv(n) = {
local(B, a1, a2, a3, a4, a6, b, E, P, m, x);
B = 20;
a4 = Mod(random(n), n);
b = 4*a4^3 + 27;
b = 1/b;
a1 = a2 = a3 = Mod(0, n);
a6 = Mod(1, n);
E = ellinit([a1, a2, a3, a4, a6]);
P = [0,1];
ellisoncurve(E, P);
m = 1;
for(i = 1, B,
m = lcm(m, i));
print(m);
x = gcd(
trap(,
ellpow(E, P, m),
ellpow(E, P, m)
), n);
}

Related

Can not get variables to pass from function to main

I can not get main to print anything besides the variables are equal to zero which means my greedy1 function is not passing my variable and I do not know what is wrong.
def greedy1(total):
if (total >= 25):
q = total // 25
remainderCentsQ = total % 25
d = remainderCentsQ // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 10):
q = 0
d = total // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 5):
q = 0
d = 0
n = total // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
else:
q = 0
d = 0
n = 0
p = total // 1
return (q, d, n, p)
def printCoins1(total, q, d, n, p):
print("For " + str(total) + " cents change, I give\n" + str(q) + " quarters\n" + str(d) + " dimes\n" + str(n) + " nickels\n" + str(p) + " pennies")
def main():
total = int(input("How many cents change? "))
q = 0
d = 0
n = 0
p = 0
greedy1(total)
printCoins1(total, q, d, n, p)
main()
Inside your main function, you're calling greedy1 function with total that returns the tuple. But you're not storing this return values back to your variables q, d, n, p.
your main() should look like this:
def main():
total = int(input("How many cents change? "))
(q, d, n, p) = greedy1(total)
printCoins1(total, q, d, n, p)
#Sayas has given the correct answer however there is some redundant code , so I will improve it in my answer
def main():
total = int(input("How many cents change? "))
printCoins1(total, *greedy1(total))

is there something wrong in my code? i am not receiving the desired output

SELECT
CASE
WHEN (A = B) AND (B = C) THEN 'Equilateral'
WHEN (A = B) OR (B = C) OR (A = C) THEN 'Isosceles'
WHEN ((A+B) < C) OR ((A+C) < B) OR ((C+B) < A) THEN 'Not A Triangle'
ELSE 'Scalene'
END
FROM TRIANGLES;
i am checking the type of triangle if length of the sides are given as A, B, C in the table TRIANGLES.
I think you SQL should be like this.
You should check Not A Triangle condition first, and you should change < operator to <= as well. And I suggest you should select A, B, C as well to make it more readable.
SELECT A, B, C,
CASE
WHEN ((A+B) <= C) OR ((A+C) <= B) OR ((C+B) <= A) THEN 'Not A Triangle'
WHEN (A = B) AND (B = C) THEN 'Equilateral'
WHEN (A = B) OR (B = C) OR (A = C) THEN 'Isosceles'
ELSE 'Scalene'
END
FROM TRIANGLES;

Haskell program simple game

I am very new in haskell-programming. I try to program a simple dice-game, but I don't know to do it in haskell.
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a₁,a₂,a₃) c (d₁,d₂,d₃)
I want to consider each difference dᵢ - aᵢ (but not if aᵢ > dᵢ) and return False if (d1-a1)+(d2-a2)+(d3-a3) are larger than c. (if aᵢ > dᵢ then I sum up 0 instead the difference)
I try something like this:
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) = ?????
where diff1 = if (d1 > a1) then d1-a1
diff2 = if (d2 > a2) then d2-a2
diff3 = if (d3 > a3) then d3-a3
Because in Haskell, else is not a optional part of an if expression, so you need to define diff1 as diff1 = if d1 > a1 then d1 - a1 else 0. Other two are similar.
Notes that > returns a Bool value, so you could just sum these three differences up and compare it with c, and use it as your condition.
There are several ways to define this function:
suc1 (a1, a2, a3) c (d1, d2, d3) = diff1 + diff2 + diff3 <= c
where diff1 = if d1 > a1 then d1 - a1 else 0
diff2 = if d2 > a2 then d2 - a2 else 0
diff3 = if d3 > a3 then d3 - a3 else 0
suc2 (a1, a2, a3) c (d1, d2, d3) = sum diffs <= c
where diff1 = max (d1-a1) 0
diff2 = max (d2-a2) 0
diff3 = max (d3-a3) 0
diffs = [diff1, diff2, diff3]
suc3 (a1, a2, a3) c (d1, d2, d3) = sum (zipWith diff as ds) <= c
where diff a d = max (d-a) 0
as = [a1, a2, a3]
ds = [d1, d2, d3]
How about this?
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) =
((if a1> d1 then 0 else d1-a1) + (if a2> d2 then 0 else d2-a2) + (if a3>d3 then 0 else d3-a3) > c )
Or alternatively
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) =
max 0 (d1-a1) + max 0 (d2-a2) + max 0 (d3-a3) > c

Excel VBA UDF Formatting - Change argument values before calculation

I am trying to write a formula in Excel VBA to calculate: RR = ((A / (A + B)) / (C / (C + D)))
When any of the four arguments (A, B, C, D) are 0, I want to change their value to 0.5 in the calculation.
Is there an easy way of doing this? I believe my formatting is wrong or I'm going about it the wrong way. Any helpful tips would be greatly appreciated.
I've tried:
Function RR(A, B, C, D) As Double
If A = 0 And B = 0 Then
A = 0.5
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 Then
A = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf B = 0 Then
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf C = 0 Then
C = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf D = 0 Then
D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 And D = 0 Then
A = 0.5 And D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
Else: RR = ((A / (A + B)) / (C / (C + D)))
End If
End Function
Try this one:
Function RR(A, B, C, D) As Double
If A = 0 Then A = 0.5
If B = 0 Then B = 0.5
If C = 0 Then C = 0.5
If D = 0 Then D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
End Function

Looping Through a1 to h8

I have some text fields inside objects a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 c2...
num = 97;
while (num <= 104) {
if (this[String.fromCharCode(num) + "1"].piece == wb1_txt) {
oldSquare = String.fromCharCode(num) + "1";
trace("oldSquare = ", oldSquare);
}
num = num + 1;
}
I want to find text field's location. How can I make it loop?
The following code will go through a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 ... g8 h8
for( i=1; i<=8; i++){
for( num=97; num<105; num++){
sq = String.fromCharCode(num) + i;
trace( sq );
}
}