Created
March 27, 2019 17:59
-
-
Save jatinsharrma/79388d5cb10d4480d461ab90e5776cfc to your computer and use it in GitHub Desktop.
Find prime number to a specific number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program takes alot of memory if given number is big. Can optimize it by using dynamic array concept | |
#include <stdio.h> | |
int print(int p[], int index){ | |
for(int i =0; i<index;i++){ | |
printf("%d\n",p[i]); | |
} | |
} | |
int prime(int x){ | |
int p[x/2+1]; | |
int index =2; | |
if (x>3){ | |
p[0] = 2; | |
p[1] = 3; | |
for (int i = 4 ; i<=x;i++){ | |
if (i%2 == 0){ | |
continue; | |
} | |
else{ | |
for (int j=0 ;j<index;j++){ | |
if(i%p[j]==0){ | |
break; | |
} | |
if (j == (index-1)){ | |
p[index] = i; | |
index ++; | |
} | |
} | |
} | |
} | |
print(p,index); | |
} | |
else if (x == 3){ | |
p[0]=2; | |
p[1]=3; | |
index = 2; | |
print(p,index); | |
} | |
else if (x == 2){ | |
p[0] = 2; | |
index = 1; | |
print(p,index); | |
} | |
else{ | |
printf("No prime found. You entered a invalid range"); | |
} | |
} | |
void main() { | |
prime(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment