首页 > 代码库 > 素数应用

素数应用

输入一个数 分解尾三个数想乘 如果三个数其中有两个为素数->yes 否则no

 1 //素数升级
 2 //功能:??已知N是正整数,?它可拆写成三个正整数N1、N2和N3之和的形式N=N1+N2+N3。?
 3 //请编程序打印出满足上式的全部组合,并当N1、N2和N3中至少有两个是素数时打印“YES”否则打印“NO”。?
 4 //如N = 5时输出:?
 5 //5 = 1 + 1 + 3?????NO?
 6 //5 = 1 + 2 + 2?????YES?
 7 //5 = 1 + 3 + 1?????NO?
 8 //5 = 2 + 1 + 2?????YES?
 9 //5 = 2 + 2 + 1?????YES?
10 //5 = 3 + 1 + 1?????NO?
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 
16 int judgePrim(int n);
17 int main()
18 {
19     int sum = 0;
20     int m = 0;
21     int i;
22     printf("请输入一个数");
23     scanf_s("%d", &m);
24     for (i = 1; i < m; i++)
25     {
26         for (int j = 1; j < m; j++)
27         {
28             int k = m - i - j;
29             if (i + j + k == m&&k>0)
30             {
31                 sum = judgePrim(i) + judgePrim(j) + judgePrim(k);
32                 printf("%d+%d+%d=%d\t", i, j, k, m);
33                 if (sum >= 2)
34                 {
35                     printf("YES\n");
36                 }
37                 else
38                 {
39                     printf("no\n");
40                 }
41             }
42             
43         }
44     }
45     system("pause");
46     return 1;
47 }
48 
49 int judgePrim(int n)
50 {
51     if (n == 1)
52     {
53         return 0;
54     }
55     else if (n == 2 || n == 3)
56     {
57         return 1;
58     }
59     else
60     {
61         for (int i = 2; i <= (int)sqrt((double)n); i++)
62         {
63             if (n%i == 0)
64                 return 0;
65         }
66     }
67     return 1;
68 }

技术分享

素数应用