iphone - how to find the permutations and combinations of any integer value? -
i want permutations , combinations in objective c.i have found both problem in code don't understand occurring. want print value in textfield prints last value entered. used code given below. me please..
typedef enum{power,exponent,ncr,npr} calcoperation; nsstring *storage; calcoperation operation; -(ibaction)combination{ operation = npr; storage = label.text; textfield.text=@""; label.text = @""; } -(int) factorial:(int)n{ int d = n; int temp = 1; for(int b = 2; b <= d; b++){ temp = temp*b; } //nslog(@"the factorials = %i",temp); return temp; } -(ibaction)equalbutton{ float tempo = [storage floatvalue]; float b = [label.text floatvalue]; nsstring *val = [[nsstring alloc]initwithformat:@"%f" , b]; switch(operation) { case ncr: textfield.text = [[nsstring alloc]initwithformat:@"%f",[self factorial:tempo] / ([self factorial:[val floatvalue]] * [self factorial:tempo-[val floatvalue]])]; //textfield.text = [[nsstring alloc]initwithformat:@"%f",[val floatvalue]+tempo+10]; nslog(@"combination %d",[self factorial:tempo] / ([self factorial:[val floatvalue]] * [self factorial:tempo-[val floatvalue]])); break; case npr: textfield.text = [[nsstring alloc]initwithformat:@"%f",[self factorial:tempo] / [self factorial:tempo-[val floatvalue]]]; nslog(@"permutations %d",[self factorial:tempo] / ([self factorial:tempo-[val floatvalue]])); break; ` } }
//to calculate ncr n = tempo, r = b
int tempo = [storage intvalue]; int b = [label.text intvalue]; float tempcb = (float)[self factorial:tempo] / ([self factorial:b] * [self factorial:tempo-b])]; break;
Comments
Post a Comment