Maple prime testing - primes

Why do the following lines of code produce no output (for d=2 and k=5 the number d^2^k is prime)
for d from 2 to 277 do:
for k from 1 to 11 do:
if isprime(d^(2^k)+1) then:
<d,k>;
end if;
end do;
end do;
ANSWER
It appears that you have to set a global parameter printlevel to the maximum depth of nested expressions which you want maple's compiler to print from i.e. in this case we have 3 nested structures so we want printlevel := 3 before we do the execution.

Related

Fortran 77 Do Loop block clarification

Both NUM and ARRAY double precision variables, not sure how the if block will execute. When will it stop? What is it actually doing?, If we go to 8, then are we exiting the do loop?
Thanks
DO 7 I = 1,28
IF (NUM - ARRAY(I)) 8,7,7
7 CONTINUE
I=29
8 NUM = ARRAY(I)
....
....
....
....
As previously stated the conditional in the loop is an arithmetic if statement.
We know (and that's explicitly stated in the previous answer here) that if num is less than array(i) the label 8 is chosen, otherwise label 7 is chosen. It's also stated in that other answer that these have the effect of exiting the loop or cycling it. To be precise, I'll continue.
A DO construct has a range. A nonblock DO construct like the one in the question has range consisting of the statements between and including the do statement and the DO termination statement (in this case 7 continue). The DO termination statement is a valid target for a jump from within the range of the construct.
When the DO termination statement is jumped to, execution remains within the scope of the construct. That termination statement is executed (in this case, continue, doing nothing) and the loop iteration condition is again tested. That is, the loop cycles.
From within a DO construct, a jump to a statement outside the range of the construct terminates execution of the construct: like an exit.
This example, then, has the equivalent form using an IF construct (with go tos - bear with me)
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
GO TO 7
END IF
7 CONTINUE
I=29
8 NUM = ARRAY(I)
Now, because the statement labelled 7 is a continue statement, we can write this as
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
CYCLE
END IF
7 CONTINUE
I=29
8 NUM = ARRAY(I)
That's still pretty ugly (and not just because of all the upper case). Fortunately, we can make this prettier. The i=29 statement will be executed only when the loop terminates without the statement labelled 8 being jumped to. Now, the loop index i has control I = 1,28 so when the loop terminates naturally the index already has the value 29. That assignment does nothing (in modern Fortran) so we can remove it. Which leaves us with
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
CYCLE
END IF
7 CONTINUE
8 NUM = ARRAY(I)
When we also note that the IF construct is immediately followed by the end of the loop (and so we don't need to explicitly cycle) we have
DO 7 I = 1,28
IF (NUM < ARRAY(I)) EXIT
7 CONTINUE
NUM = ARRAY(I)
or (more nicely)
DO I = 1,28
IF (NUM < ARRAY(I)) EXIT
END DO
NUM = ARRAY(I)
All this example is doing is finding the value of the earliest element in array(1:28) which is larger than num, or array(29) if none is.
That is called arithmetic if: if(a) 1,2,3 . and it means: if a<0 it goes to 1, if(a==0) it goes to 2 and if (a>0) it goes to 3.
in your code if( num-array(i)<0 ) it goes to 8 (exit the loop and skip another line), else it goes to 7 (cycle).

Code to Sum up the first 1234 multiples of 3 and 5 does not print anything

I'm trying to write some Fortran 90 code to sum up the first 1234 multiples of 3 and 5 (including multiples of both). Here is my code so far:
program sum
implicit none
integer :: x
integer :: y = 5
integer :: z = 3
integer :: n
if (mod(x,y) == 0 .or. mod(x,z) ==0) then
print *, x
n = x
n = x + x
end if
end program sum
However, this code does not print anything to the terminal.
Your code tests the value of x in the if condition:
if (mod(x,y) == 0 .or. mod(x,z) ==0
but the value of x is not set at all. Therefore the result of the program is completely undefined. You need to create some kind of loop. Better two loops.
The most naive approach is to loop from 1 and test all numbers with the above if condition and stop when you have found the desired number of multiples.

SAS Sum specific column that the name are stored in other column

I have a data set like this :
ID I201401 I201402 ... I201411 I201412 START END
1 1 0 1 1 I201402 I201410
2 0 0 0 1 I201401 I201408
3 1 1 0 0 I201408 I201412
To explain the dataset simply each ID have a 1 or 0 in column I201401 through I201412 depending on certain factor. Depending on other factor I establish column START and END too. Not all ID have the same START and END value.
What I want to do is to create a other column that is the summation of the column mention in the START column through the END column. For quick understanding here is what the dataset should appear :
ID SUM
1 (SUM of I201402 Throught I201410)
2 (SUM of I201401 Throught I201408)
3 (SUM of I201408 Throught I201412)
The thing is a don't really know how to specifies the sum function to use the value of column START and END to do is operation.
Thank you!
I don't know how to do this without looping, but with an array and the vname() function, you should be able to do what you need:
data want (keep=id sum);
set have;
array var_array I201401--I201412;
sum=0;
do over var_array;
if start le vname(var_array) le end then sum = sum + var_array;
end;
run;

Not Equals Constraint in PROC OPTMODEL

I have an optimization problem that I need to solve. It's a binary linear programming problem, so all of the decision variables are equal to 0 or 1. I need certain combinations of these decision variables to add up to either 0 or 2+, they cannot sum to 1. I'm struggling with how to accomplish this in PROC OPTMODEL.
Something like this is what I need:
con sum_con: x+y+z~=1;
Unfortunately, this just throws a syntax error... Is there any way to accomplish this?
See below for a linear reformulation. However, you may not need it. In SAS 9.4m2 (SAS/OR 13.2), your expression works as written. You just need to invoke the (experimental) CLP solver:
proc optmodel;
/* In SAS/OR 13.2 you can use your code directly.
Just invoke the experimental CLP solver */
var x binary, y binary, z binary;
con sum_con: x+y+z~=1;
solve with clp / findall;
print {i in 1 .. _NSOL_} x.sol[i]
{i in 1 .. _NSOL_} y.sol[i]
{i in 1 .. _NSOL_} z.sol[i];
produces immediately:
[1] x.SOL y.SOL z.SOL
1 0 0 0
2 0 1 1
3 1 0 1
4 1 1 0
5 1 1 1
In older versions of SAS/OR, you can still call PROC CLP directly,
which is not experimental.
The syntax for your example will be very similar to PROC OPTMODEL's.
I am sure, however, that your model has other variables and constraints.
In that case, remember that no matter how you formulate this,
it is still a search space with a hole in the middle.
So it potentially can make the solver perform poorly.
How poorly is hard to predict. It depends on other features of your model.
If MILP is a better fit for the rest of your model,
you can reformulate your constraint as a valid MILP in two steps.
First, add a binary variable that is zero only when the expression is zero:
/* If solve with CLP is not available, you can linearize the disjunction: */
var IsGTZero binary; /* 1 if any variable in the expression is 1 */
con IsGTZeroBoundsExpression: 3 * IsGTZero >= x + y + z;
Then add another constraint that forces the expression to be
at least the constant you want (in this case 2) when it is nonzero.
num atLeast init 2;
con ZeroOrAtLeast: x + y + z >= atLeast * IsGTZero;
min f=0; /* Explicit objectives are unnecessary in 13.2 */
solve;
The following equation should work:
(x+y-z)*z + (y+z-x)*x + (x+z-y)*y > -1
It can be generalized to more than three variables and if you have some large number you should be able to use index expansions to make it easier.

Is it prime? TI-BASIC

Hi Im trying to translate this code to TI-BASIC. Im having problems with how to change for loop into while loop and also with incrementing a number in TI-BASIC.
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
You can efficiently use a While loop in this situation:
Input "NUMBER: ",A
1->B
3->I
√(A->D
If not(fPart(A/2
DelVar BWhile I<=D and B
fPart(A/I->B
I+2->I
End
If not(B
Disp "NOT
Disp "PRIME
In TI-Basic a While loop works as you would expect and you can have conditions for it.
Incrementing a number is as simple as saying
X+i->X
Where 'i' is the incrementer.
To change a For loop into a While loop, you'll have to set up the While loop to constantly check to see if the number and increment have passed the upper bound while increasing the increment each run through.
If you wanted to mimic i++ or ++i in TI-Basic (Using a While loop), all you would have to change would be the arrangement of the code. Please note that TI-Basic For statements always operates under ++i.
Example (i++):
0->X
While X<10
Disp X
X+1->X
End
This will display (With each number on a new line)
0 1 2 3 4 5 6 7 8 9
Example (++i):
0->X
While X<10
X+1->X
Disp X
End
This will display (With each number on a new line)
1 2 3 4 5 6 7 8 9 10
Let it be noted that TI-Basic For statements are much much faster than While loops when it comes to incrementing and should almost always be considered superior for the task.
Integrating Timtech's idea to skip even numbers effectively halves the required time to check the primality of the number with the addition of only a few extra lines.
I expanded the idea to skip multiples of two and multiples of three.
Input "Number:",X:abs(X->X
0
If not(fPart(X/2)) or not(fPart(X/3:Return
For(B,5,sqrt(X),6)
If not(fPart(X/B)) or not(fPart((X+2)/B:Return
End
1
Test Number: 1003001
Time Required: ~4 Seconds (So much better than 15 :D)
Size: 65 Bytes
I dont see why you would want to use a while loop as ti-basic has for loops:
0->F
Input "ENTER NUMBER:",N
For(I,2,int(N/2
If N/I=int(N/I
Then
int(N/2->I
1->F
End
End
If F
Then
Disp "NUMBER IS PRIME
Else
Disp "NUMBER IS NOT PRIME
End
N/I=int(N/I is a way to check for a number's remainder (another way of saying N%I==0 but ti basic does not have modulus). Another trick here is setting I to its maximum bound (int(N/2) as a sort of "break" like other languages would have