• 算法15---数论6---素数,回文素数 分解质因素


    算法15---数论6---素数,回文素数  分解质因素

     1 /*
     2     题目:素数,回文素数
     3     author taoliu——alex  2016.10   number4
     4 
     5     主要实现:
     6     判断素数,判断回文素数
     7 */
     8 
     9 
    10 
    11 #include <stdio.h>
    12 #include <math.h>
    13 
    14 
    15 
    16 //素数
    17 
    18 
    19 int isprime(int a)
    20 {
    21     for (int i = 2; i < a; i++)
    22     {
    23         if (a%i==0)
    24         {
    25             return 0;
    26         }
    27 
    28     }
    29     return 1;
    30 }
    31 
    32 
    33 
    34 //判断素数,判断回文素数
    35 int huiwen(int n)
    36 {
    37     int count=0 ;//判断n的位数
    38     int k=1;
    39     int sum,num;
    40     while(k>0)
    41     {
    42         k=n-(int)pow(10,count);
    43         count++;
    44     }
    45     int weishu=count-1;
    46     for (int i = 0; i < weishu; i++)
    47     {
    48         int temp=num%10;
    49         sum=sum+temp*((int)pow(10,weishu-1-i));
    50         num=(num-temp)/10;
    51     }
    52     int t=sum;
    53     if (t==n)
    54     {
    55         if (isprime(n))
    56         {
    57             return 1;
    58         }
    59         else
    60             return 0;
    61     }
    62     else
    63         return 0;
    64 }
    65 
    66 
    67 
    68 //平方回文素数
    69 int pingfanghuiwen(int n)
    70 {
    71     int m=huiwen(n*n);
    72     if (m==1)
    73     {
    74         return 1;
    75     }
    76     else
    77         return 0;
    78 }
    79 
    80 int main()
    81 {
    82     printf("please input the data you want to judge
    ");
    83     int judge;
    84     scanf("%d",&judge);
    85     //剩下的就是判断了,就不多说了;
    86     return 0;
    87 }
     1 /*
     2     题目:分解质因子
     3     author taoliu——alex  2016.10   number4
     4 
     5 */
     6 
     7 
     8 #include <stdio.h>
     9 #include <math.h>
    10 
    11 
    12 
    13 //素数
    14 
    15 
    16 int isprime(int a)
    17 {
    18     for (int i = 2; i < a; i++)
    19     {
    20         if (a%i==0)
    21         {
    22             return 0;
    23         }
    24 
    25     }
    26     return 1;
    27 }
    28 
    29 
    30 
    31 void primerfactor(int n)
    32 {
    33     int i;
    34     if (isprime(n))
    35     {
    36         printf("%d
    ", n);
    37     }
    38     else
    39     {
    40         for ( i = 2; i <n; i++)
    41         {
    42             if (n%i==0)
    43             {
    44                 printf("%d *",i);
    45                 if (isprime(n/i))
    46                 {
    47                     printf("%d
    ",n/i);
    48                     break;
    49                 }
    50                 else
    51                     primerfactor(n/i);
    52                 break;
    53             }
    54         }
    55     }
    56 }
    57 
    58 int main()
    59 {
    60     int n;
    61     printf("please input a number 
    ");
    62     scanf("%d",&n);
    63     printf("n=%d
    ",n );
    64     printf("the factor is 
    ");
    65     primerfactor(n);
    66     return 0;
    67 }
  • 相关阅读:
    java学习笔记(4)多态
    scala学习笔记(1)
    java复习(3)继承下
    java复习(3)继承
    java复习(2)
    java中常见的异常种类
    数组的内存结构
    Castle ActiveRecord学习(一)简介
    OAuth2.0 Owin 授权问题
    将对象转为json,加入到HttpResponseMessage中
  • 原文地址:https://www.cnblogs.com/tao-alex/p/5946623.html
Copyright © 2020-2023  润新知