Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, there is a problem with huge exponent.

example if I have the formula 2 pow n and n=2 then 2 pow 2=4

and the combinations are:

00
01
10
11

but what if n=100 ??? how to write code in c# for this solution...

thnx friends.
Posted
Comments
AnkitGoel.com 10-Dec-12 4:49am    
what is the logic of generating these combinations? please explain.
[no name] 10-Dec-12 5:06am    
Elaborate your question...
h5h6 10-Dec-12 5:26am    
Yes, so there is a problem I cant write a code.
I want to write program only with that formula 2 pow n ( pow is for exponent )
I want to be generated all combinations for 2 pow 2 the result is 4
so the combinations are
00
01
10
11
for 2 pow 3 the resulat is 8
combnations:
000
001
010
101
101
011
111
110

so how to write code for this generation of combinations

You should go recursive (please note 2^100 is huge, so you need to be patient while waiting for the program to complete :-) ), e.g.
C#
static void bits(int[]  ab, int depth, int maxdept)
   {
     if (depth < maxdept)
     {
       ab[depth] = 0;
       bits(ab, depth + 1, maxdept);
       ab[depth] = 1;
       bits(ab, depth + 1, maxdept);
     }
     else
     {
       foreach (int i in ab)
       {
         Console.Write(i);
       }
       Console.WriteLine();
     }
   }

   public static void Main()
   {
     int[] ab;
     ab = new int[100];
     bits(ab, 0, 100);
   }
 
Share this answer
 
Comments
h5h6 10-Dec-12 5:31am    
haha for that, that is huge, I am looking for any other choice
CPallini 10-Dec-12 5:49am    
I think you haven't another choice.
h5h6 10-Dec-12 17:44pm    
why where i give the valu 10 ore more.... there are not all combinations....
example there is missig 000000000, 0000000001 ect,,
CPallini 10-Dec-12 18:12pm    
The program actually produces full output, starting from 0000000000 and terminating with 1111111111. Since the ouput is 2^10=1024 lines long, you probably cannot scroll your console window to see it all (console buffer is limited).
Try to redirect the program output to a file (e.g. myprogram > out.txt) and then check out the file content.
h5h6 10-Dec-12 18:26pm    
i can scroll so that is a problem....i see that there are not all generations
I assume you mean that for 2n there are 4 possible bit combinations for n == 2, 8 for n == 3, and so forth.

To display the total combinations for a fixed value of n isn't complex - you just need a set of nested loops, one for each n - so n == 2needs two loops (one for the first bit, one for the second), and n == 3 needs 3.
But for any value of n, you can't specify the number of loops in advance, so you need to look at recursion. It's not complex, but since this smells badly of homework, I won't give you the code.

This about it: create a method which takes a number and a string as a parameter. It calls itself with the string + '0' and the number less one, then calls itself with the string + '1' and the number less one. (You will need to check for a termination here, and just print the string you are passed instead)

Try it, and see what you get...
 
Share this answer
 
Comments
h5h6 10-Dec-12 16:59pm    
thnx, man.
OriginalGriff 11-Dec-12 2:47am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900