objective c - Consecutive Primes Challenge -
i working on consecutive primes challenge @ codeeval.com , can't pass automatic grader. think getting correct results possibly missing edge cases. tried recursion couldn't work , slow. figured out solution creating 2 arrays , odd numbers. please let me know if viable solution , if can see mistake.
here challenge description:
alice has number of n beads, , each bead has number 1 n painted on it. make necklace out of beads, special requirement: 2 beads next each other on necklace must sum prime number. alice needs calculate how many ways possible so.
for example:
n = 4
there 2 possible ways build necklace. note last bead connects first bead.
1 2 3 4 1 4 3 2
note: necklace should unique. example:
1 2 3 4
same2 3 4 1
,3 4 1 2
,4 1 2 3
.
the code has c , obj-c because of limitation in codeeval.com auto grader. here code:
bool isprime(int number){ for(int = 2;i<(int)(number/2)+1;i++){ if (number%i == 0) { return false; } } return true; } bool iseven(int number) { if(number%2==0){ return true; } return false; } nsmutablearray *createevennumbersarray(int num) { nsmutablearray *evennumarray = [[nsmutablearray alloc]initwithcapacity:num]; (int i=2; i<=num; i+=2) { [evennumarray addobject:[nsnumber numberwithint:i]]; } return evennumarray; } nsmutablearray *createoddnumbersarray(int num) { nsmutablearray *oddnumarray = [[nsmutablearray alloc]initwithcapacity:num]; (int i=3; i<=num; i+=2) { [oddnumarray addobject:[nsnumber numberwithint:i]]; } return oddnumarray; } int calculateconsecutive(int input) { int count = 0; bool evenfound = no; bool oddfound = no; nsmutablearray *oddnumarray = createoddnumbersarray(input); //creates odd number array possiblities nsmutablearray *evennumarray = createevennumbersarray(input); // create number array possibilities nsmutablearray *necklace = [[nsmutablearray alloc]init]; [necklace addobject:[nsnumber numberwithint:1]]; // start necklace 1 rid of duplicate necklaces (int = 2; i<=input; i+=2) { // goes through possibilities second bit create necklase nsmutablearray *tempoddnumarray = [oddnumarray mutablecopy]; // populate odd number array nsmutablearray *tempevennumarray = [evennumarray mutablecopy]; // puplate number array if(isprime([[necklace lastobject] intvalue] + i)){ [tempevennumarray removeobject:[nsnumber numberwithint:i]]; //remove number added necklase [necklace addobject:[nsnumber numberwithint:i]]; while ([necklace count]<=input) { // start creating necklace after 2 numbers added oddfound = no; evenfound = no; for(nsnumber *oddnumber in tempoddnumarray){ // find odd number possibility numbers left in array if(isprime([[necklace lastobject] intvalue] + oddnumber.intvalue)){ [necklace addobject:oddnumber]; oddfound = yes; break; } } if (!oddfound) { break; }else{ [tempoddnumarray removeobject:[necklace lastobject]]; } for(nsnumber *evennumber in tempevennumarray){ // find odd number possibility numbers left in array if(isprime([[necklace lastobject] intvalue] + evennumber.intvalue)){ [necklace addobject:evennumber]; evenfound = yes; break; } } if (!evenfound) { break; }else{ [tempevennumarray removeobject:[necklace lastobject]]; } if (([tempoddnumarray count] == 0) || ([tempevennumarray count] == 0) ){ break; } } } if (([necklace count] == input) && (isprime([[necklace lastobject] intvalue]+[[necklace firstobject] intvalue]))) { // check make sure necklace full , if sum of last number , first number prime count= count + 1; } nslog(@"%@",necklace); [necklace removeallobjects]; [necklace addobject:[nsnumber numberwithint:1]]; } return count; } -(void)primesmain { int necklacecount = 0; int input = 18; if (iseven(input)) { necklacecount = calculateconsecutive(input); } nslog(@"%d",necklacecount); } -(void)viewdidload { [self primesmain]; }
i apologize regarding crossposting code review site. wanted suggestions on code , find error. let me know , delete post on 1 of forums.
thank you!
=============== update
i found error in previous code. didn't iterate through possibilities. rewrote code using recursion. getting different answers still not correct. codeeval using 8 test value , 0 result. wonder if not correct. here updated code
nsuinteger count=0; // using global variable don't have deal counting in recursion anyway bool isprime(int number){ for(int = 2;i<(int)(number/2)+1;i++){ if (number%i == 0) { return false; } } return true; } bool iseven(int number) { if(number%2==0){ return true; } return false; } void calculatenumnecklaces(int numofbeads, nsmutablearray *beadsarray, nsmutablearray *necklace) { if ([beadsarray count] == 1) { nslog(@"%@",necklace); if((isprime([[necklace lastobject] intvalue] + [[beadsarray lastobject] intvalue]) && (isprime([[necklace objectatindex:0] intvalue] + [[beadsarray lastobject] intvalue])))){ count +=1; } //return 1; }else{ int previousnumber = [[necklace lastobject] intvalue]; int startpos = 0; if (iseven(previousnumber)){ if (iseven([[beadsarray objectatindex:0] intvalue])) { startpos = 1; //it itterate through odd numbers in bead array starting position odd number } }else { if (!iseven([[beadsarray objectatindex:0] intvalue])) { startpos = 1; //it itterate through numbers in bead array starting position odd number } } (int pos = startpos; pos < [beadsarray count]; pos+=2) { if (isprime(previousnumber + [[beadsarray objectatindex:pos] intvalue])) { nsmutablearray *tempnecklace = [necklace mutablecopy]; [tempnecklace addobject:[beadsarray objectatindex:pos]]; nsmutablearray *temparray = [beadsarray mutablecopy]; [temparray removeobjectatindex:pos]; //nslog(@"%@",necklace); calculatenumnecklaces(numofbeads, temparray, tempnecklace); } } } //return 0; } -(void)primesmain { int input = 6; if (iseven(input)) { nsmutablearray *beadsarray =[[nsmutablearray alloc] init]; (int = 2; i<=input; i++) { [beadsarray addobject:[nsnumber numberwithint:i]]; } nsmutablearray *necklace = [[nsmutablearray alloc]init]; [necklace addobject:[nsnumber numberwithint:1]]; calculatenumnecklaces(input, beadsarray, necklace); } nslog(@"%d",count); }
this program finding how many possibilities of consecutive numbers(rummy in cards) in array....
program:
#include<iostream.h> #include<conio.h> void main() { int a[10],j,i,n,c,d; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { if(a[j]>a[j+1]) { int temp; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for(j=1;j<=n;j++) { c=a[j]-a[j+1]; d=a[j]-a[j+2]; if(c==-1 && d==-2) { cout<<a[j]<<a[j+1]<<a[j+2]; } cout<<"\n"; } getch(); } output: input: a[i]={4,3,6,7,8,5} output: 345 456 567 678
Comments
Post a Comment