First Consecutive composites Help
Hello everybody !
Here is my problem...I have to make a program to present the first consecutive composites numbering from 1 to .....
The first group of Consecutive composites should apart from 1 composites
The second group of Consecutive composites should apart from 3 composites
The third group of Consecutive composites should apart from 5 composites
.......................
The last group of Consecutive composites should apart from N composites (N is given).
But every group should always be the first to contain x Consecutive composites.
e.g.
Look below to imagine what i mean.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101
This is what my program should print for N=131:
First 1 consecutive composites from 4 to 4
First 3 consecutive composites from 8 to 10
First 5 consecutive composites from 24 to 28
First 7 consecutive composites from 90 to 96
First 9 consecutive composites from 140 to 148
First 11 consecutive composites from 200 to 210
First 13 consecutive composites from 114 to 126
First 15 consecutive composites from 1832 to 1846
First 17 consecutive composites from 524 to 540
.................................................. ...........
First 43 consecutive composites from 15684 to 15726
First 45 consecutive composites from 81464 to 81508
.................................................. ...........
First 127 consecutive composites from 3851460 to 3851586
First 129 consecutive composites from 5518688 to 5518816
First 131 consecutive composites from 1357202 to 1357332
I try this, I've made so many changes to work properly but nothing.
My program print consecutive composites but not the first.
My Code:
Code:
#include <stdio.h>
#include <math.h>
#define N 101 //N is given
int iscomp(int); //Function that checks if a number is composite
int main(void)
{
int i,temp_count,j=4;
for(i=1;i<=N;i+=2){ //Start loop from 1 to N
temp_count=0;
for(;temp_count<i;j++){ //Loop until temp_count equals to i which means that i nums writen
if((j%2==0)||(iscomp(j))){ //check for composites number
temp_count++; //Increase temp_count if yes and go to the next loop
continue;}
temp_count=0; //Or zero temp_count if not
}
printf("First %d consecutive composites from %d %d\n",i,j-temp_count,j-1);
}
putchar('\n');
return 0;
}
int iscomp(int k){ //Function for checking if a num is composite or not
int i;
if(k%2==0)
return 1;
for(i=3;i<=sqrt(k);i++)
if(k%i==0)
return 1;
return 0;
}
I know that i have to run the loop from the start but i can achieve it. A wrong always appear!!!
Thank anyway