-
Notifications
You must be signed in to change notification settings - Fork 105
/
s0vM6NqY.cpp
58 lines (56 loc) · 984 Bytes
/
s0vM6NqY.cpp
1
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
/*
Sưu tầm bởi @nguyenvanhieuvn
Thực hành nhiều bài tập hơn tại https://luyencode.net/
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int isPrime(long n)
{
if(n < 2)
{
return 0;
}
else if(n > 2)
{
if(n % 2 == 0)
{
return 0;
}
for(int i = 3; i <= sqrt((float)n); i += 2)
{
if(n % i == 0)
{
return 0;
}
}
}
return 1;
}
void selectPrime(FILE* fpIn, FILE* fpOut)
{
long p;
while(fscanf(fpIn, "%ld", &p) > 0)
{
if(isPrime(p))
{
fprintf(fpOut, "%ld\t", p);
}
}
}
void main()
{
int n;
FILE* fpIn = fopen("Numbers.txt", "rt");
FILE* fpOut = fopen("Primes.txt", "wt");
if(fpIn != NULL)
{
if(fpOut != NULL)
{
selectPrime(fpIn, fpOut);
fclose(fpOut);
}
fclose(fpIn);
}
getch();
}