### Author Topic: [ENDED] Code Golf Contest #3  (Read 12340 times)

0 Members and 1 Guest are viewing this topic.

#### JWinslow23

• Coder Of Tomorrow
• LV7 Elite (Next: 700)
• Posts: 556
• Rating: +43/-6
• I make quality calculator games...when I have time
##### [ENDED] Code Golf Contest #3
« on: July 28, 2014, 03:29:31 pm »
This is the same deal as the other two contests.

NEXT: Here
PREVIOUS: Here

Challenge 3

Problem
Make a program that, given an integer of reasonable size, outputs the greatest prime factor of that integer, in binary, but with all 0s replaced with underscores (_) and all 1s replaced with minus signs (-).

August 4, 2014, 1:00 AM EST

Sample input 1
15
Sample output 1
-_-
Sample input 2
7
Sample output 2
---
Sample input 3
115
Sample output 3
-_---

If any further clarification is necessary, contact me or willrandship, and we will explain the best we can.

Ranking

TI-83+ BASIC
 Rank User Size Date Code 1 Runer112 75 8/3/2014 2:17:19 PM Spoiler For Spoiler: For(A,Ans,2,~1If not(fPart(Ans/AA->PEnd"BFor(A,0,log(P)/log(2sub("_-",iPart(2fPart(P/2/2^A))+1,1)+AnsEndsub(Ans,1,A 2 JWinslow23 90 7/29/2014 11:43:19 AM Spoiler For Spoiler: Ans->XX=1->AWhile X>12->AWhile fPart(X/AIS>(A,XEndX/A->XEnd"_If A" Ans->Str1While A"_If fPart(.5A"-Ans+Str1->Str1int(.5A->AEndStr1

Python2
 Rank User Size Date Code 1 willrandship 147 7/29/2014 12:51:41 AM Spoiler For Spoiler: def f(z): for y in range(2,z+1):  if z%y:continue  return z if y==z else f(z/y)a=""for c in bin(f(input()))[2:]:a+='-'if c=='1'else'_'print a

Ruby
 Rank User Size Date Code 1 Juju 115 7/29/2014 2:47:50 AM Spoiler For Spoiler: a=gets.to_f;b=2;while a>1;(c=a/b)==c.to_i&&(a=c)||b+=1 end;p b.to_s(2).gsub('0','_').gsub('1','-')

Nspire Lua
 Rank User Size Date Code 1 Jens_K 132 8/1/2014 6:59:33 AM Spoiler For Spoiler: n=0+clipboard.getText()f=nrepeat f=(f>2 and f-1 or n)until n%f<1b=""repeat b=(f%2>0 and"-"or"_")..b;f=(f-f%2)/2 until f<1print(b)

SysRPL
 Rank User Size Date Code 1 3298 59.5 7/31/2014 5:18:48 PM Spoiler For Spoiler: ::  FPTR2 ^NFactorSpc  DUPLENCOMP  NTHCOMPDROP  FPTR2 ^Z>#  NULL$SWAP BEGIN DUP #2/ UNROT BINT1 #AND #1= ITE CHR_- CHR_UndScore >T$  SWAP #0=UNTIL DROP;

Perl
 Rank User Size Date Code 1 willrandship 98 7/31/2014 2:15:31 PM Spoiler For Spoiler: $z=<>;sub f{for(2..$z){$z/=$z%$_?next:$_;return$z>1?&f:$_;}}$_=sprintf"%b",f;s/1/-/g;s/0/_/g;print Haskell  Rank User Size Date Code 1 3298 122 8/3/2014 6:16:11 PM Spoiler For Spoiler: import Numericg i=showIntAtBase 2c(f(i,2))""f(1,j)=jf(i,j)=if mod i j==0then f(quot i j,j)else f(i,j+1)c 0='_'c 1='-' 2 bb010g 216 8/2/2014 7:11:01 PM Spoiler For Spoiler: import Numeric;import Data.Char;main=fmap(\n->concatMap(\case{'0'->"_";_->"-"})$showIntAtBase 2intToDigit(last[x|x<-[1..n-1],nmodx==0,elem x[n|n<-[2..x],not\$elem n[j*k|j<-[2..n-1],k<-[2..n-1]]]])"")readLn>>=putStr

CJam
 Rank User Size Date Code 1 Runer112 16 8/3/2014 2:17:19 PM Spoiler For Spoiler: q~mfZ=2b{"_-"=}%

Golfscript
 Rank User Size Date Code 1 Runer112 33 8/3/2014 2:17:19 PM Spoiler For Spoiler: ~.,2>-1%{].~%!=}/2base{"_-"=}%""+

Java
 Rank User Size Date Code 1 Runer112 172 8/3/2014 2:17:19 PM Spoiler For Spoiler: class C{public static void main(String[]a){long x=Long.decode(a[0]),i=x;while(i-->2)x=(x%i)==0?i:x;System.out.print(Long.toString(x,2).replace('0','_').replace('1','-'));}} 2 3298 185 8/3/2014 6:16:11 PM Spoiler For Spoiler: class G{public static void main(String[]c){int n=Integer.parseInt(c[0]),i=2;while(i

TI-83+ z80
 Rank User Size Date Code 1 Runer112 58 8/3/2014 2:17:19 PM Spoiler For Spoiler: ;#SECTION "MAIN", CODE   org   userMem - 2   db   0BBh, 6DhStart:   B_CALL   _RclAns   B_CALL   _ConvOP1   ld   h, d   ld   l, eTrialDivideLoop:   push   de   push   hl   B_CALL   _DivHLByDE   ld   a, h   or   l   pop   hl   pop   de   jq   nz, NotFactor   ld   h, d   ld   l, eNotFactor:   dec   de   ld   a, e   dec   a   or   d   jq   nz, TrialDivideLoop   ld   b, h   ld   c, l   ld   hl, OP1 + 15   ld   (hl), dBitLoop:   dec   hl   srl   b   rr   c   ld   (hl), '_'   jq   nc, BitUnset   ld   (hl), '-'   ld   d, h   ld   e, lBitUnset:   jq   nz, BitLoop   ex   de, hl   B_CALL   _PutS   B_CALL   _NewLine   ret

XTend
 Rank User Size Date Code 1 3298 179 8/3/2014 6:16:11 PM Spoiler For Spoiler: class G{def static void main(String[]c){var n=Integer.parseInt(c.get(0))var i=2while(i

Language Ranking

 Rank Lang User Size Date 1 CJam Runer112 16 8/3/2014 2:17:19 PM 2 Golfscript Runer112 33 8/3/2014 2:17:19 PM 3 TI-83+ z80 Runer112 58 8/3/2014 2:17:19 PM 4 SysRPL 3298 59.5 (don't ask me why; I'm going off of what he said) 7/31/2014 5:18:48 PM 5 TI-83+ BASIC Runer112 75 8/3/2014 2:17:19 PM 6 Ruby Juju 98 7/30/2014 12:01:58 AM 7 Perl willrandship 98 7/31/2014 2:15:31 PM 8 Haskell 3298 122 8/3/2014 6:16:11 PM 9 Nspire Lua Jens_K 132 8/1/2014 6:59:33 AM 10 Python2 willrandship 147 7/29/2014 12:51:41 AM 11 Java Runer112 172 8/3/2014 2:17:19 PM 12 XTend 3298 179 8/3/2014 6:16:11 PM
« Last Edit: June 11, 2015, 08:58:23 am by pimathbrainiac »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
= ?

#### Juju

• Incredibly sexy mare
• Coder Of Tomorrow
• LV13 Extreme Addict (Next: 9001)
• Posts: 5730
• Rating: +500/-19
• Weird programmer
##### Re: Code Golf Contest #3
« Reply #1 on: July 28, 2014, 03:32:59 pm »
Ooh, interesting challenge here.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?

This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

#### willrandship

• Omnimagus of the Multi-Base.
• LV11 Super Veteran (Next: 3000)
• Posts: 2953
• Rating: +98/-13
• Insert sugar to begin programming subroutine.
##### Re: Code Golf Contest #3
« Reply #2 on: July 28, 2014, 07:34:47 pm »
By the way, if your language does not support underscores (ONLY if it does not) you may use a decimal [.] symbol instead.

#### Runer112

• Moderator
• LV11 Super Veteran (Next: 3000)
• Posts: 2289
• Rating: +639/-31
##### Re: Code Golf Contest #3
« Reply #3 on: July 28, 2014, 07:36:49 pm »
By the way, if your language does not support underscores (ONLY if it does not) you may use a decimal [.] symbol instead.

For the record, 83+ BASIC does support underscores (see this post). For consistent scoring of such entries, I'd suggest that all 83+ BASIC entries should be scored as if they used underscores (two byte tokens) rather than periods.
« Last Edit: July 28, 2014, 07:39:59 pm by Runer112 »

#### willrandship

• Omnimagus of the Multi-Base.
• LV11 Super Veteran (Next: 3000)
• Posts: 2953
• Rating: +98/-13
• Insert sugar to begin programming subroutine.
##### Re: Code Golf Contest #3
« Reply #4 on: July 28, 2014, 09:10:22 pm »
Now everything's more complicated :\ stupid 2-byte tokens.

#### JWinslow23

• Coder Of Tomorrow
• LV7 Elite (Next: 700)
• Posts: 556
• Rating: +43/-6
• I make quality calculator games...when I have time
##### Re: Code Golf Contest #3
« Reply #5 on: July 28, 2014, 10:56:09 pm »
Now everything's more complicated :\ stupid 2-byte tokens.
It's not complicated at all. I did it in 122 bytes. The prime factorization code was no problem. Neither was the binary conversion. It assumes anything 1 and under are valid.

EDIT: I totally changed my greatest prime factor algorithm so lists are not used, and I have 90 bytes! Update tomorrow.
« Last Edit: July 29, 2014, 03:14:34 am by JWinslow23 »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
= ?

#### DJ Omnimaga

• Clacualters are teh gr33t
• CoT Emeritus
• LV15 Omnimagician (Next: --)
• Posts: 55942
• Rating: +3154/-232
• CodeWalrus founder & retired Omnimaga founder
##### Re: Code Golf Contest #3
« Reply #6 on: July 29, 2014, 05:31:30 pm »
Interesting challenge. I feel bad for Casio PRIZM ASM/C participants, though (good luck making an add-in under 29 KB large)

#### Juju

• Incredibly sexy mare
• Coder Of Tomorrow
• LV13 Extreme Addict (Next: 9001)
• Posts: 5730
• Rating: +500/-19
• Weird programmer
##### Re: Code Golf Contest #3
« Reply #7 on: July 30, 2014, 12:23:38 am »
I changed my algorithm as well, only basic operations are used, so I got it down to 98 bytes

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?

This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

#### calc84maniac

• eZ80 Guru
• Coder Of Tomorrow
• LV11 Super Veteran (Next: 3000)
• Posts: 2912
• Rating: +471/-17
##### Re: Code Golf Contest #3
« Reply #8 on: July 30, 2014, 02:09:34 pm »
So, I have a couple of questions.

Can we assume the input is at least 2? Otherwise, largest prime factor doesn't actually exist.
Also, can my output have a trailing space? That makes things easier in TI-BASIC because of silly string limitations.

If yes to both of these, then I have a 67-byte TI-BASIC solution.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

#### Princetonlion.tibd

• Members
• Posts: 790
• Rating: +3/-4
##### Re: Code Golf Contest #3
« Reply #9 on: July 30, 2014, 02:18:20 pm »
I feel like using 68k BASIC would be cheating, it has binary mode
I'm going to at least try, not sure if I'll manage to PM it though

#### JWinslow23

• Coder Of Tomorrow
• LV7 Elite (Next: 700)
• Posts: 556
• Rating: +43/-6
• I make quality calculator games...when I have time
##### Re: Code Golf Contest #3
« Reply #10 on: July 30, 2014, 03:58:54 pm »
So, I have a couple of questions.

Can we assume the input is at least 2? Otherwise, largest prime factor doesn't actually exist.
Also, can my output have a trailing space? That makes things easier in TI-BASIC because of silly string limitations.

If yes to both of these, then I have a 67-byte TI-BASIC solution.
I actually don't know the rules on valid input. My 90 byte solution assumes input is any integer. Ask willrandship for confirmation, I don't know.
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
= ?

#### Runer112

• Moderator
• LV11 Super Veteran (Next: 3000)
• Posts: 2289
• Rating: +639/-31
##### Re: Code Golf Contest #3
« Reply #11 on: July 30, 2014, 04:11:48 pm »
So, I have a couple of questions.

Can we assume the input is at least 2? Otherwise, largest prime factor doesn't actually exist.
Also, can my output have a trailing space? That makes things easier in TI-BASIC because of silly string limitations.

If yes to both of these, then I have a 67-byte TI-BASIC solution.

Assuming that the input is at least 2 and an integer is fine, since any other numbers do not have a greatest prime factor. But I'd be of the opinion that a trailing space is not okay, especially in the case of TI-BASIC, in which the trailing space would become obvious when doing something like recalling or otherwise operating on Ans.

#### 3298

• LV2 Member (Next: 40)
• Posts: 26
• Rating: +1/-0
##### Re: Code Golf Contest #3
« Reply #12 on: July 30, 2014, 06:37:55 pm »
Any entries below 60 bytes yet? I just submitted a 59.5-byte solution in SysRPL. (SysRPL runs on the HP calculators featuring a Saturn processor, which is a 4-bit one. That explains why half bytes are possible as a size there.)
My code calls some OS code to get a sorted list of prime factors (6 bytes, because that call is a flashpointer), and then takes the last element of that list (2 commands for 2.5 bytes each). Thus the assumptions are the same as the OS code's, which should be any integer >=1. (I don't know if 0 is fine as well, but I don't feel like risking a calculator crash to find out.)

#### Princetonlion.tibd

• Members
• Posts: 790
• Rating: +3/-4
##### Re: Code Golf Contest #3
« Reply #13 on: July 30, 2014, 07:01:24 pm »
Mine is 100 something bytes, and it doesn't work yet. As you can tell, I still haven't learned to optimize 68k BASIC

#### willrandship

• Omnimagus of the Multi-Base.
• LV11 Super Veteran (Next: 3000)
• Posts: 2953
• Rating: +98/-13
• Insert sugar to begin programming subroutine.
##### Re: Code Golf Contest #3
« Reply #14 on: July 31, 2014, 02:30:22 am »
Yes, assuming >=2 is totally fine. IMO, code golf should be about handling the meat of the question, not about handling all the wacky edge cases. If ignoring edge cases makes a smaller solution, that's what I want to see.