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

0 Members and 1 Guest are viewing this topic.

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
[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 (-).

Deadline
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
RankUserSizeDateCode
1Runer112758/3/2014 2:17:19 PM
Spoiler For Spoiler:
For(A,Ans,2,~1
If not(fPart(Ans/A
A->P
End
"B
For(A,0,log(P)/log(2
sub("_-",iPart(2fPart(P/2/2^A))+1,1)+Ans
End
sub(Ans,1,A
2JWinslow23907/29/2014 11:43:19 AM
Spoiler For Spoiler:
Ans->X
X=1->A
While X>1
2->A
While fPart(X/A
IS>(A,X
End
X/A->X
End
"_
If A
"
Ans->Str1
While A
"_
If fPart(.5A
"-
Ans+Str1->Str1
int(.5A->A
End
Str1

Python2
RankUserSizeDateCode
1willrandship1477/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
RankUserSizeDateCode
1Juju1157/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
RankUserSizeDateCode
1Jens_K1328/1/2014 6:59:33 AM
Spoiler For Spoiler:
n=0+clipboard.getText()f=n
repeat f=(f>2 and f-1 or n)until n%f<1
b=""repeat b=(f%2>0 and"-"or"_")..b;f=(f-f%2)/2 until f<1
print(b)

SysRPL
RankUserSizeDateCode
1329859.57/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
RankUserSizeDateCode
1willrandship987/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
RankUserSizeDateCode
132981228/3/2014 6:16:11 PM
Spoiler For Spoiler:
import Numeric
g i=showIntAtBase 2c(f(i,2))""
f(1,j)=j
f(i,j)=if mod i j==0then f(quot i j,j)else f(i,j+1)
c 0='_'
c 1='-'
2bb010g2168/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],n`mod`x==0,elem x[n|n<-[2..x],not$elem n[j*k|j<-[2..n-1],k<-[2..n-1]]]])"")readLn>>=putStr

CJam
RankUserSizeDateCode
1Runer112168/3/2014 2:17:19 PM
Spoiler For Spoiler:
q~mfZ=2b{"_-"=}%

Golfscript
RankUserSizeDateCode
1Runer112338/3/2014 2:17:19 PM
Spoiler For Spoiler:
~.,2>-1%{].~%!=}/2base{"_-"=}%""+

Java
RankUserSizeDateCode
1Runer1121728/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','-'));}}
232981858/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<n)if(n%i==0)n/=i;else++i;System.out.print(Integer.toString(n,2).replace('0','_').replace('1','-'));}}

TI-83+ z80
RankUserSizeDateCode
1Runer112588/3/2014 2:17:19 PM
Spoiler For Spoiler:
;#SECTION "MAIN", CODE

   org   userMem - 2
   db   0BBh, 6Dh
Start:
   B_CALL   _RclAns
   B_CALL   _ConvOP1
   ld   h, d
   ld   l, e
TrialDivideLoop:
   push   de
   push   hl
   B_CALL   _DivHLByDE
   ld   a, h
   or   l
   pop   hl
   pop   de
   jq   nz, NotFactor
   ld   h, d
   ld   l, e
NotFactor:
   dec   de
   ld   a, e
   dec   a
   or   d
   jq   nz, TrialDivideLoop
   ld   b, h
   ld   c, l
   ld   hl, OP1 + 15
   ld   (hl), d
BitLoop:
   dec   hl
   srl   b
   rr   c
   ld   (hl), '_'
   jq   nc, BitUnset
   ld   (hl), '-'
   ld   d, h
   ld   e, l
BitUnset:
   jq   nz, BitLoop
   ex   de, hl
   B_CALL   _PutS
   B_CALL   _NewLine
   ret

XTend
RankUserSizeDateCode
132981798/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<n)if(n%i==0)n=n/i else i=i+1print(Integer.toString(n,2).replace('0','_').replace('1','-'))}}

Language Ranking

RankLangUserSizeDate
1CJamRuner112168/3/2014 2:17:19 PM
2GolfscriptRuner112338/3/2014 2:17:19 PM
3TI-83+ z80Runer112588/3/2014 2:17:19 PM
4SysRPL329859.5 (don't ask me why; I'm going off of what he said)7/31/2014 5:18:48 PM
5TI-83+ BASICRuner112758/3/2014 2:17:19 PM
6RubyJuju987/30/2014 12:01:58 AM
7Perlwillrandship987/31/2014 2:15:31 PM
8Haskell32981228/3/2014 6:16:11 PM
9Nspire LuaJens_K1328/1/2014 6:59:33 AM
10Python2willrandship1477/29/2014 12:51:41 AM
11JavaRuner1121728/3/2014 2:17:19 PM
12XTend32981798/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"?
Click here for the only set of games you'll ever need
= ?

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
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?
THEGAME
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.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
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.

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
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 »

Offline willrandship

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

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
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! :D Update tomorrow.
« Last Edit: July 29, 2014, 03:14:34 am by JWinslow23 »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
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 :P (good luck making an add-in under 29 KB large)

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
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 :3

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
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.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
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

Offline Princetonlion.tibd

  • Members
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 790
  • Rating: +3/-4
    • View Profile
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 :P
I'm going to at least try, not sure if I'll manage to PM it though

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
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"?
Click here for the only set of games you'll ever need
= ?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
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.

Offline 3298

  • LV2 Member (Next: 40)
  • **
  • Posts: 26
  • Rating: +1/-0
    • View Profile
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.)

Offline Princetonlion.tibd

  • Members
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 790
  • Rating: +3/-4
    • View Profile
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 :P

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
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.