高手们,都来看看啦
一道题目:对已知的自然数n,设计一个判断是否为素数的函数,并调用该函数实现将100-200区间的偶数分解为两个素数和的形式。谢谢
程序代码:
#include<math.h> unsigned int m_stack[200]; int m_stack_tal = 0; struct number{ unsigned int m_left_value; unsigned int m_right_value; }; struct bruker{ number m_number_value[20]; unsigned int m_number_tab; }; bruker m_bruker[100]; bool _primeJudge(unsigned int m_i){ if(m_i<4){ if(m_i==1) return 0; return 1; } else{ if(m_i%2==0) return 0; else{ int j = sqrt((double)m_i); for(int i=3;i<=j;i+=2){ if(m_i%i==0) return 0; } return 1; } } } int main() { for(int i=2;i<=200;i++){ if(_primeJudge(i)){ m_stack[m_stack_tal++] = i; printf("%d是素数",i); } } int sum; for(int i=1;i<m_stack_tal;i++){ for(int j=m_stack_tal-1;j>=i;j--){ sum = m_stack[i] + m_stack[j]; if(sum>200) continue; else if(sum<100) break; else{ int x = sum-101; int y = m_bruker[x].m_number_tab; m_bruker[x].m_number_value[y].m_left_value = m_stack[i]; m_bruker[x].m_number_value[y].m_right_value = m_stack[j]; m_bruker[x].m_number_tab++; } } } for(int i=0;i<100;i++){ if(m_bruker[i].m_number_tab==0) printf("%d该值不能分解为2个素数的和\n",i+101); for(int j=m_bruker[i].m_number_tab-1;j>=0;j--){ printf("%d 该值可分解为 %d + %d (%d)\n",i+101,m_bruker[i].m_number_value[j].m_left_value,m_bruker[i].m_number_value[j].m_right_value,j); } } return 0; }