求素数或者素数因子

学完C++写的求素数的代码:

求指定范围内的素数 综合使用循环语句.cpp

#include<iostream>
#include<cmath>
#include <cstdlib>
using namespace std;
int main()
{   int i=0,m=0,k=0,j=0;
    int t=0,n=0;
    //指定下限
    cout<<"Please enter lower limit of search ranges: ";
    cin>>t;
    //指定上限
    cout<<"Please enter upper limit of search ranges: ";
    cin>>n;
    cout<<"The results are: "<<endl;
    if(t>n)
        cout<<"Sorry,your lower limit number is bigger than upper limit number! "<<endl;
    else
    {   //判断否为素数,判断依据在t-n之间没有可以被整除的数
        for(m=t;m<=n;m++)
        {   k=int(sqrt(double(m)));
            i=2;
            while(m%i&&i<=k) i++;
            if(i>k&&m!=1)
            {    cout<<m<<"  ";
                 j++;
                 if(j%10==0) cout<<"\n";
            }
        }
        cout<<"\nTotal number : "<<j<<endl;
    }
    system("pause");
    main();
}

 

用该方法求一个整数的素数因子

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int answer=0;
 9     cin >> answer;
10     int  m = answer; //用输入的数初始化m,防止原来的数丢失
11     //因为因子不会大于m/2,在2~m/2之间找因子
12     for(int i=2;i <= answer / 2;i++)
13     {
14         if(m%i==0)
15         {
16             bool b=true;
17             //判断数i是否为素数,判断依据在2~√i 之间没有可以被整除的数
18             for(int j=2;j <= sqrt((double)i);j++)
19                 //如果有在2~√i 之间有可以被i整除的数则排除该数
20                 if(i % j ==0)
21                     b = false;
22             //在上一个检查过程中没有一个在2~√i 之间有可以被i整除的数则该数是素数
23             if(b == true)
24                 cout<<i<<" ";
25         }
26     }
27     cout<<endl;
28     system("pause");
29     main();
30 }

在java中

 1 import javax.swing.JOptionPane;
 2 public class chapter1 {
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         String input = JOptionPane.showInputDialog(null, "Please enter a integer:" ," ",JOptionPane.INFORMATION_MESSAGE);
 6         String text = "";
 7         if(input.length()==0)
 8             JOptionPane.showMessageDialog(null, "Please enter a number","Error",JOptionPane.ERROR_MESSAGE);
 9         else{
10             int answer=Integer.parseInt(input);
11             int  m=answer; //用输入的数初始化m,防止原来的数丢失
12             //因为因子不会大于m/2,在2~m/2之间找因子
13             for(int i=2;i <= answer / 2;i++) {
14                 boolean b=true;
15                 //判断数i是否为素数
16                 for(int j=2;j <= Math.pow( (double)i, 0.5);j++)
17                     //如果有在2~√i 之间有可以被i整除的数则排除该数
18                     if(i % j ==0)
19                         b = false;
20                 //在上一个检查过程中没有一个在2~√i 之间有可以被i整除的数则该数是素数
21                 if(b == true) {
22                     //检出因子并存入text
23                     while(m % i == 0 ) {
24                         text += i + " ";
25                         m=m / i;
26                     }
27                 }
28             }
29             if(text.length()==0)
30                 text="Sorry,the number you enter doesn't have factor !";
31             JOptionPane.showMessageDialog(null,text);
32         }
33     }
34 }

  今天九天突然问到了,写下来,以后参考一下。其实总结起来,这几个都是围绕着一个算法来的。

转载于:https://www.cnblogs.com/brock-1993/p/3400676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值