Author |
Message |
|
Galandros
Active Member
Joined: 29 Aug 2008 Posts: 565
|
Posted: 09 Nov 2009 12:38:20 pm Post subject: |
|
|
Objective:
Generate all possible binary numbers and store in a matrix variable. Each row has a number.
Input:
A is number of bits
B is 2^A
EDIT: B may not be used but because this fits in a real program, the variable is needed for something else.
Examples:
Input: A=1
Output: [A]=[[1,0]]
Input: A=2
Output: [A]=
[[1,0]
[1,1]
[0,0]
[0,1]]
The order of the numbers doesn't matter, it just needs to be all possible present.
Depending on the outcome of the teaser, I can suggest a variation where order matters.
I already have an answer, it is 66 bytes.
EDIT: this routine is used in one of my programs... so the teaser came from there.
Last edited by Guest on 09 Nov 2009 01:00:48 pm; edited 1 time in total |
|
Back to top |
|
|
ah-blabla
Newbie
Joined: 28 Oct 2009 Posts: 26
|
Posted: 09 Nov 2009 01:28:48 pm Post subject: |
|
|
151 bytes: (68K code...):
[whiteout]
bin(a)
Func
If a=1 Then
Return {"0","1"}
EndIf
Local l,e,i
{}->l
bin(a-1)->e
For i,1,dim(e)
"0"&e[i]->l[dim(l)+1]
"1"&e[i]->l[dim(l)+1]
EndFor
l
EndFunc
[/whiteout]
(Another 11 bytes could be saved with the Local declaration.)
Ooops... Just realised you want a matrix.
Last edited by Guest on 12 Jul 2010 01:06:09 am; edited 1 time in total |
|
Back to top |
|
|
Flofloflo
Member
Joined: 07 Nov 2007 Posts: 120
|
Posted: 09 Nov 2009 01:30:35 pm Post subject: |
|
|
I currently have a version, where the matrix is made within the program, with a size of 88 bytes. Does input and matrix-setup have to be within the program? |
|
Back to top |
|
|
Galandros
Active Member
Joined: 29 Aug 2008 Posts: 565
|
Posted: 09 Nov 2009 01:41:03 pm Post subject: |
|
|
Hmm maybe we have 68k teaser here, too. If someone joined ah-blabla...
Flofloflo wrote: I currently have a version, where the matrix is made within the program, with a size of 88 bytes. Does input and matrix-setup have to be within the program?
The input is not needed. A and B hold their values on program entry.
You must set up the matrix. It shouldn't be any errors produced by matrix not defined, dim mismatch and others.
Basically: the matrix doesn't exist or it will get overwritten.
Last edited by Guest on 09 Nov 2009 01:45:11 pm; edited 1 time in total |
|
Back to top |
|
|
darkstone knight
Advanced Member
Joined: 07 Sep 2008 Posts: 438
|
Posted: 09 Nov 2009 02:53:34 pm Post subject: |
|
|
cant you just fill the matrix using randint(0,1 untill you have all solutions? |
|
Back to top |
|
|
Galandros
Active Member
Joined: 29 Aug 2008 Posts: 565
|
Posted: 09 Nov 2009 05:10:57 pm Post subject: |
|
|
darkstone knight wrote: cant you just fill the matrix using randint(0,1 untill you have all solutions?
Yes, if you can assure with 100% and not 99,99999999% sure, then yes.
Now more seriously, repetitions aren't allowed, too. |
|
Back to top |
|
|
Builderboy2005
Advanced Newbie
Joined: 19 Apr 2009 Posts: 51
|
Posted: 09 Nov 2009 05:43:42 pm Post subject: |
|
|
I have a 53 Byte version with program name A |
|
Back to top |
|
|
IAmACalculator In a state of quasi-hiatus
Know-It-All
Joined: 21 Oct 2005 Posts: 1571
|
Posted: 10 Nov 2009 06:16:31 am Post subject: |
|
|
Is setting the base to binary (68k only) cheating? Because that would reduce the problem down to a simple seq( .
Last edited by Guest on 01 Jul 2010 10:32:25 am; edited 1 time in total |
|
Back to top |
|
|
ah-blabla
Newbie
Joined: 28 Oct 2009 Posts: 26
|
Posted: 10 Nov 2009 06:46:21 am Post subject: |
|
|
IAmACalculator wrote: Is setting the base to binary (68k only) cheating? Because that would reduce the problem down to a simple seq( . That's not really a challenge though is it? (I completely forgot about that possibility... )
BTW, I wrote a matrix version on the 68K, name bn(), size 169bytes. However using A=10 causes a memory error, and A=8 takes ages... It seems A=4 is the border for quick answers.
Last edited by Guest on 01 Jul 2010 10:32:41 am; edited 1 time in total |
|
Back to top |
|
|
Galandros
Active Member
Joined: 29 Aug 2008 Posts: 565
|
Posted: 10 Nov 2009 09:41:24 am Post subject: |
|
|
IAmACalculator wrote: Is setting the base to binary (68k only) cheating? Because that would reduce the problem down to a simple seq( .
Well, if you can store the bits into elements of matrix (see examples), that is using calc capabilities and for this teaser it isn't cheating.
But 68k entries only compete with 68k ones.
@Builderboy I am curious :)
Well here is my answer:
Quote:
:For(I,1,A
:2^I
:List>matr(.5Ans>AnsfPart(seq(X-1,X,1,B )/Ans),[B]
:If I=1
:Then
:[B]->[A]
:Else
:augment([B],[A]->[A]
:End
:End
Now thinking of if maybe there is room for optimization...
Last edited by Guest on 01 Jul 2010 10:32:08 am; edited 1 time in total |
|
Back to top |
|
|
Builderboy2005
Advanced Newbie
Joined: 19 Apr 2009 Posts: 51
|
Posted: 10 Nov 2009 10:31:04 am Post subject: |
|
|
Alright, here it is, whittled down to 52 bytes
[whiteout]{B,A->dim([A]
For(F,1,A
For(G,1,B
iPart(2fPart(g/2^f->[A](G,F
End
End
[/whiteout]
Not sure it could get any smaller than that, but I could be wrong, Weregoose hasn't posted yet XD
Last edited by Guest on 12 Jul 2010 01:04:49 am; edited 1 time in total |
|
Back to top |
|
|
Weregoose Authentic INTJ
Super Elite (Last Title)
Joined: 25 Nov 2004 Posts: 3976
|
Posted: 12 Nov 2009 02:30:32 pm Post subject: |
|
|
It seems that we have already tackled size, so I went for speed. The matrix area (more than) doubles with each pass:
Spoiler wrote: [font="courier new"][[0][1
For(X,2,A
List►matr(cumSum(binomcdf(2^X-1,0))-1,[A]
int(augment(2/2^X[A],augment(Ans[font="arial"]т,Ans[font="arial"]т)[font="arial"]т
End
Last edited by Guest on 12 Nov 2009 04:01:25 pm; edited 1 time in total |
|
Back to top |
|
|
ztrumpet
Active Member
Joined: 06 May 2009 Posts: 555
|
Posted: 12 Nov 2009 04:49:11 pm Post subject: |
|
|
Wow! I like how you took advantage of List>matr not returning a value to Ans. Cool!
Last edited by Guest on 12 Nov 2009 04:49:29 pm; edited 1 time in total |
|
Back to top |
|
|
Galandros
Active Member
Joined: 29 Aug 2008 Posts: 565
|
Posted: 13 Nov 2009 09:22:56 am Post subject: |
|
|
For my program it is useful to have the binary possibilities following the order/rule on this example:
111
110
101
100
011
010
001
000
My routine accomplish this. Actually people when want to do all binary possibilities this seems the best way to not forget 1 possibility.
Can you optimize mine in size? It is fast enough.
Last edited by Guest on 13 Nov 2009 09:24:04 am; edited 1 time in total |
|
Back to top |
|
|
|