Calculator Community > Community Contests

[ENDED] Code Golf Contest #3

(1/5) > >>

JWinslow23:
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
RankUserSizeDateCode1Runer112758/3/2014 2:17:19 PMSpoiler 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,A2JWinslow23907/29/2014 11:43:19 AMSpoiler 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
RankUserSizeDateCode1willrandship1477/29/2014 12:51:41 AMSpoiler 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
RankUserSizeDateCode1Juju1157/29/2014 2:47:50 AMSpoiler 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
RankUserSizeDateCode1Jens_K1328/1/2014 6:59:33 AMSpoiler 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
RankUserSizeDateCode1329859.57/31/2014 5:18:48 PMSpoiler 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
RankUserSizeDateCode1willrandship987/31/2014 2:15:31 PMSpoiler 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
RankUserSizeDateCode132981228/3/2014 6:16:11 PMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode1Runer112168/3/2014 2:17:19 PMSpoiler For Spoiler: q~mfZ=2b{"_-"=}%
Golfscript
RankUserSizeDateCode1Runer112338/3/2014 2:17:19 PMSpoiler For Spoiler: ~.,2>-1%{].~%!=}/2base{"_-"=}%""+
Java
RankUserSizeDateCode1Runer1121728/3/2014 2:17:19 PMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode1Runer112588/3/2014 2:17:19 PMSpoiler 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
RankUserSizeDateCode132981798/3/2014 6:16:11 PMSpoiler 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

RankLangUserSizeDate1CJamRuner112168/3/2014 2:17:19 PM2GolfscriptRuner112338/3/2014 2:17:19 PM3TI-83+ z80Runer112588/3/2014 2:17:19 PM4SysRPL329859.5 (don't ask me why; I'm going off of what he said)7/31/2014 5:18:48 PM5TI-83+ BASICRuner112758/3/2014 2:17:19 PM6RubyJuju987/30/2014 12:01:58 AM7Perlwillrandship987/31/2014 2:15:31 PM8Haskell32981228/3/2014 6:16:11 PM9Nspire LuaJens_K1328/1/2014 6:59:33 AM10Python2willrandship1477/29/2014 12:51:41 AM11JavaRuner1121728/3/2014 2:17:19 PM12XTend32981798/3/2014 6:16:11 PM

Juju:
Ooh, interesting challenge here.

willrandship:
By the way, if your language does not support underscores (ONLY if it does not) you may use a decimal [.] symbol instead.

Runer112:

--- Quote from: willrandship 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.

--- End quote ---

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.

willrandship:
Now everything's more complicated :\ stupid 2-byte tokens.

Navigation

[0] Message Index

[#] Next page

Go to full version