Modular Exponentiation for high numbers in C++

前端 未结 6 1729
失恋的感觉
失恋的感觉 2020-12-01 12:48

So I\'ve been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun pr

6条回答
  •  爱一瞬间的悲伤
    2020-12-01 13:20

        package playTime;
    
        public class play {
    
            public static long count = 0; 
            public static long binSlots = 10; 
            public static long y = 645; 
            public static long finalValue = 1; 
            public static long x = 11; 
    
            public static void main(String[] args){
    
                int[] binArray = new int[]{0,0,1,0,0,0,0,1,0,1};  
    
                x = BME(x, count, binArray); 
    
                System.out.print("\nfinal value:"+finalValue);
    
            }
    
            public static long BME(long x, long count, int[] binArray){
    
                if(count == binSlots){
                    return finalValue; 
                }
    
                if(binArray[(int) count] == 1){
                    finalValue = finalValue*x%y; 
                }
    
                x = (x*x)%y; 
                System.out.print("Array("+binArray[(int) count]+") "
                                +"x("+x+")" +" finalVal("+              finalValue + ")\n");
    
                count++; 
    
    
                return BME(x, count,binArray); 
            }
    
        }
    

提交回复
热议问题