need a Maple program - maple

Write a Maple code in order to find all the at most three digits Pythagorean triples (a, b, c), for
a, b, c > 0. We say that an integer triple(a, b, c) is a Pythagorean triple if a^2+b^2=c^2.
Hint: You might need to use the command type(sqrt(x),integer) as it returns true if x is a complete square. Get help for type from the help center.

one possibility. You can try to make it more efficient if needed.
result:=Array(1..999);
n:=0;
for a from 1 to 999 do
for b from a to 999 do
c:=sqrt(a^2+b^2);
if type(c,integer) and length(c)<=3 then
n:=n+1;
result(n):=[a,b,c];
fi;
od;
od;
result:=result(1..n);
To print them
for item in result do
print(item[1]^`2`+item[2]^`2`=item[3]^`2`)
od
....

Consider this:
isolve(a^2+b^2=c^2);

Related

How can one drop/delete columns from a KDB table in place?

Following the documentation, I tried to do the following:
t:([]a:1 2 3;b:4 5 6;c:`d`e`f) // some input table
`a`b _ t // works: delete NOT in place
(enlist `a) _ t // works: delete NOT in place
t _:`a`b // drop columns in place does not work; how to make it to work?
// 'type
// [0] t _:`a`b
Thank you very much for your help!
You should be able to use
delete a,b from `t
to delete in place (The backtick implies in place).
Alternatively, for more flexibility you could use the functional form;
![`t;();0b;`a`b]
The simplest way to achieve column deletion in place is using qSQL:
t:([]a:1 2 3;b:4 5 6;c:`d`e`f)
delete a,b from `t -- here, the backtick before t makes the change in place.
q)t
c
-
d
e
f
Michael & Kyle have covered the q-SQL options; for completeness, here are a couple of other options using _:
Using _ as in your question, you can re-assign this back to t e.g.
t:`a`b _ t
You can also use . amend with an empty list of indexes i.e. "amend entire", which can be done in-place by passing `t or not in-place by passing just t e.g.
q).[t;();`a`b _] / not in-place
c
-
d
e
f
q).[`t;();`a`b _] / in-place
`t
q)t
c
-
d
e
f

kdb apply function in select by row

I have a table
t: flip `S`V ! ((`$"|A|B|"; `$"|B|C|D|"; `$"|B|"); 1 2 3)
and some dicts
t1: 4 10 15 20 ! 1 2 3 5;
t2: 4 10 15 20 ! 0.5 2 4 5;
Now I need to add a column with values on the the substrings in S and the function below (which is a bit pseudocode because I am stuck here).
f:{[s;v];
if[`A in "|" vs string s; t:t1;];
else if[`B in "|" vs string s; t:t2;];
k: asc key t;
:t k k binr v;
}
problems are that s and v are passed in as full column vectors when I do something like
update l:f[S,V] from t;
How can I make this an operation that works by row?
How can I make this a vectorized function?
Thanks
You will want to use the each-both adverb to apply a function over two columns by row.
In your case:
update l:f'[S;V] from t;
To help with your pseudocode function, you might want to use $, the if-else operator, e.g.
f:{[s;v]
t:$["A"in ls:"|"vs string s;t1;"B"in ls;t2;()!()];
k:asc key t;
:t k k binr v;
};
You've not mentioned a final else clause in your pseudocode but $ expects one hence the empty dictionary at the end.
Also note that in your table the columns S and V have been cast to a symbol. vs expects a string to split so I've had to use the stringoperation - this could be removed if you are able to redefine your original table.
Hope this helps!

Replacement of numbers

I am given three variables having finite values ( all are integers) m,n, r.
Now I need to do m<-r and n<-r ( assign m and n the value of r ) and I have read in "The Art of Computer Programming vol. 1 " that the operations can be combined as
m<-n<-r
But will the above statement not mean "assign m the value of n and then n the value of r".
Thanks in advance.
The order of assignment is from right to left. Thus, m<-n<-r will be interpreted as: n<-r and then m<-n.
Since n equals r after the first assignment, m<-n and m<-r are identical.
Assignment = operator is like assigning the right side value to left side. For eg
int a = 1 + 2;
Here first 1+2 is evaluated and assigned to a because it follows right to left associativity.
Now if you have something like this
int a=b=2;
It again follows right to left associativity. From right first b=2 is evaluated and assign 2 to b then b is assigned to a. It works like this a=(b=2)
Know in your question you have m<-n<-r . This will work like this m<-(n<-r)
You can see reference Operator Associativity

Matlab: regexp usage

I am going to start illustration using a code:
A = 'G1(General G1Airlines american G1Fungus )';
Using regexp (or any other function) in Matlab I want to distinctively locate: G1, G1A and G1F.
Currently if I try to do something as:
B = regexp( A, 'G1')
It is not able to distinguish G1 with the G1A and G1F i.e. I need to force the comparison to find me only case with G1 and ignore G1A and G1F.
However, when I am searching for G1A then it should still find me the location of G1A.
Can someone please help ?
Edit: Another case for A is:
A = 'R1George Service SmalR1Al C&I)';
And the expression this time I need to find is R1 and R1A instead.
Edit:
I have a giant array containing A's and another big vector containing G1, R1, etc I need to search for.
If you want to find 'G1' but not 'G1A' or 'G1F' you can use
>> B = regexp(A, 'G1[^AF]')
B =
1
This will find 'G1' and the ^ is used to specify that it should not match any characters contained with []. Then you could use
>> B = regexp(A, 'G1[AF]')
B =
12 32
to find both 'G1A' and 'G1F'.

Maple: simplifying Im(a+I*b) - why it does not work for me?

So I want to simplify z:=a+I*b; Im(z) where a, b are real variables So I try:
s:= 1+2*I
Im(s) // outputs 2
z:=a+I*b
Im(z) // outputs Im(a+I*b)
So I wonder is it any how possible to simplify Im(z) so to get b as output (here we look at general case meaning z could be any complex expression from real values (like a, b, c etc and complex I))?
You didn't tell Maple that a and b were real, so the simplification doesn't work because it doesn't necessarily hold. One way to get what you want is by using the assume command to let it know:
> s:=1+2*I;
s := 1 + 2 I
> Im(s);
2
> z:=a+I*b;
z := a + b I
> Im(z);
Im(a + b I)
> assume(a,real);
> assume(b,real);
> z;
a~ + b~ I
> Im(z);
b~
The evalc command works by considering unknowns as being real.
z:=a+I*b:
Im(z);
Im(a + I b)
evalc( Im(z) );
b
See its help-page, ?evalc.