Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
1
vote
Determine if a number is divisible by 13 (without using 13 itself)
In Q:
d:{$[0=x mod "I"$((string 6h$"q")[1 2]);`yes;`no]}
50*.9=45
0
votes
First-n Fibonacci sequence elements
Q 24
f:{{x,sum -2#x}/[x;0 1]}
First n fibonacci numbers
2
votes
Mathematical Combination
Q (50 45)
f:{(prd 1.+til x)%((prd 1.+til y)*prd 1.+til x-y)}
You can shave a few characters off the above by removing redundant brackets and using 1*/ instead of prd.
f:{(1*/1.+til x)%(1*/1.+til …
0
votes
Is it Christmas?
Q (60)
system"t 1000";.z.ts:{if[.z.d~2012.12.25;1"It’s Christmas"]}
1
vote
Shortest way to reverse a number
in Q (20 characters)
f:{"I"$reverse -3!x}
Sample Usage:
q)f 89478237
73287498
Use the k version of reverse (wrapped in parentheses) to make it shorter
{"I"$(|:) -3!x}
also just define it as a …
3
votes
196 algorithm code golf
In Q (39 characters)
f:{while[x<>g:"I"$reverse -3!x;x+:g];x}
Sample Usage:
q)f 5280
23232
Edit:
Down to 34 now, same usage:
{while[x<>g:"I"$(|) -3!x;x+:g];x} 5280
1
vote
Enumerate all palindromic numbers (in decimal) between 0 and n
Q (33)
{if[x="I"$(|:) -3!x;:x]} each til
Probably a neater way to do this but anyway, sample usage (you enter n+1 to get to n):
q){if[x="I"$(|:) -3!x;:x]} each til 10
0 1 2 3 4 5 6 7 8 9
Sugges …
2
votes
Sum of primes between given range
In Q (95):
d:{sum s:{if[2=x;:x];if[1=x;:0];$[0=x mod 2;0;0=min x mod 2+til floor sqrt x;0;x]}each x+til y}
Sample Usage:
q)d[1;100]
1060
3
votes
Code-Golf: Permutations
in Q (48)
g:{$[x=1;y;raze .z.s[x-1;y]{x,/:y except x}\:y]}
Sample usage:
q)g[3;1 2 3]
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
6
votes
Generate Pascal's triangle
in Q (25 characters/20 with shorter version)
t:{(x-1) (p:{0+':x,0})\1}
Shorter
t:{(x-1){0+':x,0}\1}
Sample usage:
q)t 4
1
1 1
1 2 1
1 3 3 1
1
vote
How to randomize letters in a word
Q (42 Characters)
{(x[0]),((neg count 1_-1_x)?1_-1_x),-1#x}
Sample Usage:
q){(x[0]),((neg count 1_-1_x)?1_-1_x),-1#x} "elephant"
"eeahlnpt"
q){(x[0]),((neg count 1_-1_x)?1_-1_x),-1#x} "ant"
"ant"
…
1
vote
Print the alphabet four times
Q (68)
.Q.a,"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz",(|:).Q.a
Outputs:
"abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfg
crlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba …
6
votes
Can you Golf Golf?
Q (25 characters)
Original (27)
while[72<>sum a:18?3 4 5];a
Sample output
4 4 3 3 4 5 4 3 4 5 5 3 5 5 5 4 3 3
Slightly shorter (25)
{72<>sum x}{x:18?3 4 5}/0
1
vote
Find the Factorial!
In Q (18 characters)
f:{(*/)9h$1+til x}
Computes in less than one millisecond.
q)\t f 125
0