This question is about the GAP and the GAP programming language:
I would like to write code that will compute the following formula:
(n^+1)/GcdInt(n^2+1, 2^(Tau(n^2+1)));
for the first 100,000 integers n.
Here is what I have so far:
f:=function(n);
f:=(n^+1)/GcdInt(n^2+1, 2^(Tau(n^2+1)));
return f;
end;
It is grossly wrong. I cannot find any good sources on writing GAP code that I can understand unfortunately.
I got it:
for i in [1..100000] do
Print((i^2+1)/GcdInt(i^2+1,2^Tau(i^2+1)), " ");
od;
Print( "\n" );
Related
I was ask to do a program in Prolog which has to be able to do a summation of pair and odd numbers with a recursive structure. The rule has the following form:
sum(N,PairSum,OddSum)
With N as the number given as parameter.
For Example: if N=5 then PairSum=4+2 and OddSum=5+3+1
My Code is the following
suma(0,0,0).
suma(N,SumPares,SumImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( (0 is mod(N,2))
-> SumPares is SumaP + (N-2)
; SumImpares is SumaI +(N-2)
).
The code compiles successfully, it fails when i run it. For example with N=5
suma(5,SumaPares,SumaImpares)
I get the following
ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR:
[12] _9750 is _9756+(2-2)
You should see 2 singleton warnings when consulting this code: SumaI and SumI only appear once in the body of second clause of procedure summation/3.
Looking at your code it seems that you wanted to use the same variable, so rename SumaI to SumI. You should now see no singleton warnings after reconsulting the code.
[edit after clarification]
After your edit you still have singleton warnings. Aside from typos the problem is that your conditional branch assigns one of the sum variables and leaves the other as-is. I believe you want something like this:
suma(0,0,0).
suma(N,SumaPares,SumaImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( 0 is mod(N,2) ->
(SumaPares is SumaP + (N-2), SumaImpares=SumaI)
; (SumaImpares is SumaI +(N-2), SumaPares=SumaP)
).
After following the advices from #gusbro and inluding some others i could make to this code to work properly. The key was in the base case that was suma(2,2,1) and not suma(0,0,0). Other problem i had was my summations in the if clause were I a do N-2 and I just have to leave the N alone and not substract anything. Here is the working code:
suma(2,2,1).
suma(N,SumaPares,SumaImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( 0 is mod(N,2) ->
(SumaPares is SumaP + N, SumaImpares is SumaI)
; (SumaImpares is SumaI +N, SumaPares is SumaP)
).
Thanks a lot for the help.
Is it possible to compare two vectors in GAMS directly? For example,
set i 'plants' /i0*i9/
parameter a(i),b(i);
a(i)=uniformint(1,9);
b(i)=uniformint(1,9);
if(a(i)=b(i),
display a;
else
display b;
);
Directly using this code will result in errors. However, is it possible to use comparison of two vectors in my if condition?
Thanks, Sophia
While your conditional expression looks right, there is a loop over i missing.
This could look like the following:
Loop (i,
if(a(i)=b(i),
display a;
else
display b;
);
);
Ok, recursion is giving me "coders block" I have 2 homework assignments this week, one of them is simply the following..
Design a function that accepts an integer argument and returns the sum of all the integers from
1 up to the number passed as an argument. For example, if 50 is
passed
as an argument, the fu
nction
will return the sum
of 1, 2, 3, 4,
...
.50
. Use the recursion to calculate the sum.
This is my solution... am I understanding how this works? The book is teaching us via some fake psuedocode so I know it's not "real" code....
Function Integer SumAll(Integer Number)
If Number > 0 Then
Return Number + SumAll(Number-1)
Else
Return 0
End If
End Function
OK I think this is it, I wrote this in c++ to test it out.
#include <iostream>
using namespace std;
int SumAll(int Number){
if (Number > 0) {
return Number + SumAll(Number-1);
}
}
int main(){
cout<<SumAll(2);
return 0;
}
Your C++ code is buggy because the base case needs to be treated in the recursive function itself.
Your pseudocode is basically correct except that it doesn't work when Number < 0; you should really have an error message in that case.
I agree with the comments that you cannot possibly work in the dark; use a real programming language. Your pseudocode looks a lot like Pascal, so maybe you can use Free Pascal here. Otherwise, I'd recommend Python.
In Python 3, the code would look like this:
def sum_all(number):
if number > 1:
return number + sum_all(number-1)
elif number == 1: # base case
return 1
else:
raise Exception("negative number")
print(sum_all(10))
which would correctly return 55.
When I made my studies, the teacher also used some kind of pseudocode similar to Pascal. We agreed very early on that I would use Turbo Pascal instead, and that helped me a lot because I was able to test things on the PC.
Below is the code for my program. I'm attempting to find the value of the integral of 1/ln(x), and then evaluate the integral from 0 to x, with this as the integrand. I'm not exactly sure what I'm doing wrong, but I am quite new to Scilab.
t = input("t");
x=10; while x<t, x=x+10,
function y=f(x), y=(1/(log (x))), endfunction
I=intg(2,x,f);
function z=g(x), z=I, endfunction
W = intg(0,x,z);
W
end
I'm not entirely sure on what you are trying to achieve, but I reformatted your code and added some suggestions to documentation.
Maybe it will help you in finding the answer.
While loop
You can convert your while loop to a for loop
Your code
x=10;
while x<t
x=x+10
//some code
end
Could be
for x=10:10:t
//some code
end
Functions
In your code, you redeclare the two functions every single iteration of the while loop. You could declare them outside the while loop and call them inside the loop.
Reformatted
t = input("Please provide t: ");
// The function of 1/ln(x)
function y=f(x), y=1/log(x), endfunction
// Every time g(x) is called the current value of I is returned
function z=g(x), z=I, endfunction
for x=10:10:t
//Find definite integral of function f from 2 to x
I = intg(2,x,f);
//Find definite integral of I from 0 to x
W = intg(0,x,g);
disp( string(W) );
end
I know the question is porbably outdated; but the topic is still active. And I was looking for a code with double integral.
Here, it looks strange to use "intg" just to calculate the area of the rectangle defined by its diagonal ((0,0), (x,I)): the result is just x*I...
May be the initial aim was to consider "I" as a function of "x" (but in this case there is a convergence problem at x=1...); so restricting the integration of "I" to something above 1 gives the following code:
x=10:10:100;W2=integrate('integrate(''1/log(x2)'',''x2'',2,x1)','x1',1.001,x);
Note the use of integration variables x1 and x2, plus the use of quotes...
I just solved the first problem from Project Euler in JavaFX for the fun of it and wondered what block expressions are actually good for? Why are they superior to functions? Is it the because of the narrowed scope? Less to write? Performance?
Here's the Euler example. I used a block here but I don't know if it actually makes sense
// sums up all number from low to high exclusive which are divisible by a or b
function sumDivisibleBy(a: Integer, b: Integer, high: Integer) {
def low = if (a <= b) a else b;
def sum = {
var result = 0;
for (i in [low .. <high] where i mod a == 0 or i mod b == 0) {
result += i
}
result
}
}
Does a block make sense here?
Well, no, not really, it looks like extra complexity with no real benefit. Try removing the sum variable and the block and you will see that the code still works the same.
In general block expressions can be useful when you want to create an anonymous scope rather than factoring the code into a function, most of the time you should rather create a function.