- Aggregated Pivot Table
- Bit Shifting
- Bitwise XOR
- Calculate Tick Size
- Collapsing Dictionaries
- Graphs - Breadth First Search
- Graphs - Depth First Search
- Graphs - Detecting Cycles
- Greatest Common Divisor
- Identity Matrix
- Least Common Multiple
- Log Parser
- Nanosleep
- One Hot Encoding
- Parent Child ID Mapping
- Slippage
- Split Training and Testing Sets
- Stratified Sampling
- Symbol Column Update
- Table Indexing
- Word Count
Greatest Common Divisor
Question: Define a function 'gcd' that returns the greatest common divisor of two or more integers, as an integer.
More Information:
https://en.wikipedia.org/wiki/Greatest_common_divisorExample
q)gcd 10 5 15
5
q)gcd 5*til 100000
5
q)gcd 1+5*til 100000
1
Solution
######## solution.q ########
gcd:{$[y=0;x;.z.s[y;x mod y]]}/
Explanation: To compute GCD between two numbers, get the remainder between the first and second number. If it is 0, then the greatest common divisor is the second number, otherwise get the remainder again between the second number and the new remainder. Keep doing this recursively until your remainder is 0. To apply this over a list, use the over adverb '/'.