当前位置: 代码迷 >> Java相关 >> 急!!麻烦大虾们 帮我看下程序 自己写 却掉入了无限循环当中
  详细解决方案

急!!麻烦大虾们 帮我看下程序 自己写 却掉入了无限循环当中

热度:75   发布时间:2011-01-16 15:40:44.0
急!!麻烦大虾们 帮我看下程序 自己写 却掉入了无限循环当中
当w的w次方等于w的mod(n)时  就输出!!

import java.math.BigInteger;
import java.util.Scanner;
public class cSong {
   

        public static void main(String[] args) {
            
            for(int j=0;j<100;j++){
             Scanner reader=new Scanner(System.in);
             System.out.println("Please input n:");
             BigInteger n=new BigInteger(reader.next());
             BigInteger a=new BigInteger("1");
             BigInteger b=a;
             BigInteger w = b;
             if(isPrime(n.intValue())){
               
                 for(w=a;w.intValue()<n.intValue();w.add(a)){
                     
                     BigInteger m;
                    m=w.pow(w.intValue());
                    
                 if(m.equals(w.divideAndRemainder(n))){
                     System.out.println(w+"的"+w+"幂次方"+"="+w+"mod"+n);
                 }
                 }
             }else {
                System.out.println("n不是素数");
             }
    }
}
        public static boolean isPrime(int n)
        {
            if(n < 2) return false;

            for(int i = 2; i < n; ++i)
                if(n%i == 0) return false;
            return true;
        }
}
搜索更多相关的解决方案: public  import  

----------------解决方案--------------------------------------------------------
import java.math.BigInteger;
import java.util.Scanner;
public class cSong
{
    public static void main(String[] args)
        {
            for(int j=0;j<100;j++)
            {
             Scanner reader=new Scanner(System.in);
             System.out.println("Please input n:");
             BigInteger n=new BigInteger(reader.next());
             BigInteger a=new BigInteger("1");
             BigInteger b=a;
             BigInteger w = b;
             if(isPrime(n.intValue()))
                {
                   for(w=a;w.intValue()<n.intValue();w.add(a))
                  {
                    BigInteger m=new BigInteger("0");
                    m=w.pow(w.intValue());
                    if(m.equals(w.divideAndRemainder(n)))
                     {
                     System.out.println(w+"的"+w+"幂次方"+"="+w+"mod"+n);
                     }
                 }
             }else
               {
                System.out.println("n不是素数");
               }
        }
      }
        public static boolean isPrime(int n)
        {
            if(n < 2) return false;

            for(int i = 2; i < n; ++i)
                if(n%i == 0) return false;
            return true;
        }
}
----------------解决方案--------------------------------------------------------
  相关解决方案