![]() |
|
[Golfing] Multiplication Tables - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: [Golfing] Multiplication Tables (/Thread-Golfing-Multiplication-Tables) Pages:
1
2
|
[Golfing] Multiplication Tables - Inori - 09-08-2015 Back in primary school, formatted times tables were our best friends when learning how to multiply. Today, I present you with the golfing challenge of creating a method or function that outputs a multiplication table to n * n. You'll find an example in my solution, as well as in my CLI calculator (which isn't golfed, for easier understanding). Spoiler: tips
- String formatting (which Ruby and Python have built-in, I'm unsure about other languages) is a HUGE simplifier here. - Your function call will not impact your byte count My solution (Ruby, 67B): Code: def t(n)n=(1..n);n.each{|r|n.each{|c|print"%-3d "%(c*r)}puts""};endSample Output: ![]() RE: [Golfing] Multiplication Tables - DarkKnight - 09-08-2015 Code: n=range(1,13)
for t in n:print'%3d '*12%tuple(t*r for r in n)RE: [Golfing] Multiplication Tables - Shebang - 09-08-2015 (09-08-2015, 09:21 PM)DarkKnight Wrote: Should be a function and take input as a number. My solution (69 Bytes) in Python 2: Code: def f(n):
l=range(1,n+1)
for k in l:print'\t'.join(`k*j`for j in l)RE: [Golfing] Multiplication Tables - Inori - 09-08-2015 (09-08-2015, 09:21 PM)DarkKnight Wrote: it's on the right track, but like @Shebang said, and as I stated in the description, it needs to be a function. something like this could work: Code: def x(n): for t in n:print'%3d '*12%tuple(t*r for r in n)but it's your entry, so I won't mess with it RE: [Golfing] Multiplication Tables - Eclipse - 09-08-2015 (09-08-2015, 09:31 PM)Chitoge Wrote: it's on the right track, but like @Shebang said, and as I stated in the description, it needs to be a function. Actually, you said either a method or function. I don't really see why a function is a necessity, but meh. (09-08-2015, 09:23 PM)Shebang Wrote: Should be a function and take input as a number. This fails to execute for me? Syntax error. Not even a traceback. RE: [Golfing] Multiplication Tables - Inori - 09-08-2015 (09-08-2015, 10:02 PM)Eclipse Wrote: This fails to execute for me? Syntax error. Not even a traceback. It's some weird ascii thing. If you retype it, it works RE: [Golfing] Multiplication Tables - Shebang - 09-08-2015 (09-08-2015, 10:02 PM)Eclipse Wrote: Actually, you said either a method or function. I don't really see why a function is a necessity, but meh. The code block was removing spaces on the last lines, so I had to use another invisible char to get it to look right. Just replace with spaces. RE: [Golfing] Multiplication Tables - Penis - 10-20-2015 I really really want to try this. God damnit, why must I suck \m/ RE: [Golfing] Multiplication Tables - 0xDEAD10CC - 10-25-2015 Here's my solution in C... Relevant code (I see others didn't post the full program source): Code: int r=0,n=1;while((r=(r+1)%13)?printf("%3d ",r*n):(puts(""),++n<13));![]() 69 bytes In C, and it competes with the best solutions so far.EDIT: Code: int r=0,k=1;while((++r,r%=13)?printf("%3d ",r*k):(puts(""),++k<13));**68 bytes now RE: [Golfing] Multiplication Tables - Shebang - 10-26-2015 (10-25-2015, 07:23 PM)0xDEAD10CC Wrote: Here's my solution in C... Relevant code (I see others didn't post the full program source): Your program should be taking input somewhere, so the 13 you have hardcoded should be a number the user inputs (from what I understand). |