Last active
June 10, 2018 15:02
-
-
Save horitaku1124/7a97a1ae0bb9b070f9d8fd0f96e59e9b to your computer and use it in GitHub Desktop.
Finding prime number from StandardInput
This file contains hidden or 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
CC=gcc | |
CFLAGS=-O3 | |
all: prime pi | |
pi: | |
$(CC) $(CFLAGS) -o $@ pi.c | |
prime: | |
$(CC) $(CFLAGS) -o $@ prime.c -lm | |
clean: | |
$(RM) prime pi | |
test: prime | |
./pi | ./prime |
This file contains hidden or 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
#include <stdio.h> | |
int main(){ | |
int a=10000; | |
int b; | |
int c = 2800; | |
int d; | |
int e; | |
int f[2801]; | |
int g; | |
for(;b-c;) | |
f[b++]=a/5; | |
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) | |
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <stdbool.h> | |
inline bool isPrime(int num) | |
{ | |
if (num < 2) return false; | |
if (num == 2) return true; | |
if (num > 2 && num % 2 == 0) return false; | |
int end = ceil(sqrt(num)); | |
for (int i = 3;i <= end;i += 2) { | |
if (num % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char inputBuf[2048]; | |
scanf("%s", inputBuf); | |
// printf("len=%lu\n", strlen(inputBuf)); | |
char *numbers = (char*)malloc(sizeof(char) * strlen(inputBuf)); | |
size_t limit = strlen(inputBuf) - 2; | |
for (size_t i = 2;i < limit;i++) { | |
numbers[i - 2] = inputBuf[i] - '0'; | |
} | |
// for (size_t i = 0;i < limit;i++) { | |
// printf("%i", numbers[i]); | |
// if ((i + 1) % 50 == 0) { | |
// puts(""); | |
// } | |
// } | |
short digit = 4; | |
if (argc > 1) { | |
digit = atoi(argv[1]); | |
} | |
for (size_t i = digit - 1;i < limit;i++) { | |
char c = numbers[i]; | |
int num = 0; | |
for (int j = digit - 1;j >= 0;j--) { | |
num *= 10; | |
num += numbers[i - j]; | |
} | |
if (isPrime(num)) { | |
printf("@%lu prime => %d\n", (i + 1), num); | |
} | |
} | |
free(numbers); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment