Difference between 'exec' and 'select'; prime factorization algorithm

First, I can't understand the difference between exec and select in kdb. The reference for exec doesn't really help much!

Second, I'm trying to write a program to solve Euler problem 5. I solved it by using mod and each, but its slow and requires a ton of space. I believe I can do it by adding up the values of 20's prime factorization. Is that correct? If so, can anyone assist me in doing so? I've written the following to get the prime factorization. It can't take in a list yet.

f1:{(x mod y) = 0}
f2:{if[f1[a;y]&(not max f1[y;x],0);x:x,y];x}
f3:{x+:1;y:f2[y;x];if[x<a;y:.z.s[x;y]];y}
f3[1;()
3

3 Answers

On your first question, exec is essentially a more general form of select. A select query will return what you query for in a table format whereas an exec is more flexible and can return any type of data you want from a table. For example, if we have a table

t:([]a:10?`aa`bb`cc;b:10?10)

Then we can use exec to get a list of column data from the table

q)exec a from t
`cc`aa`aa`aa`bb`aa`bb`cc`cc`cc

which comes in very handy at times. Also

q)exec a,b from t
a| cc aa aa aa bb aa bb cc cc cc
b| 6 4 1 3 3 7 8 2 1 4
q)exec a!b from t
cc| 6
aa| 4
aa| 1
aa| 3
bb| 3
aa| 7
bb| 8
cc| 2
cc| 1
cc| 4

which extracts dictionaries. The first takes the column names as the keys and columns as values and the second takes column a a the key and column b as the value. A nice little example of these in use can be seen in creating a pivot table .

Finally, you can get the full table using

exec by 0b from t

which is equivalent to

select from t

The by 0b is implicit in the select statement. In fact, you can see this by looking at the parse trees of both.

q)parse"exec from t"
?
`t
()
()
()
q)parse"select from t"
?
`t
()
0b
()
1

The simple solution for this problem will be :-

LCM(1,2,3,......20)=1*5*7*8*9*11*13*17*19=116396280;

So,develop your code for an LCM program which will give LCM of n numbers...

2

Regarding your Second Issue, you can also use Euclid's algorithm for fast solution.

Euclid's Algo. is used to find GCD of 2 numbers and it is very fast. It uses the property that :

GCD (a;b)= GCD (b; a mod b). This goes on till (a mod b) return 0 and once that happens, 'b' is the gcd.

Reference (Section GCD):

LCM can be find using GCD.

 LCM of a&b = (a*b) / GCD of a & b

Also, one more formula that would be required is:

 LCM of a & b & c = LCM of (LCM of a & b) & c

Using the above algo and logic,here is what I tried:

 q) gcd:{[a;b] first {(x[1];x[0] mod x[1])}/[{0<>x[1]};(a;b)]} q )lcm:{[l]{floor (x*y) % gcd[x;y]}/[l]} q) lcm[1_til 10]

output: 2520

 q) lcm[1_til 20]

output: 232792560

 q) \ts lcm[1_til 20]

output: 0 864

It takes less than 1ms (approx .15ms) for calculation.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like