求n以内的最大质数

package codePra;

import org.junit.Test;

//求n以内的最大质数

public class helloword {
    @Test
    public void testFunc(){
        int res = maxPrime(10);
        System.out.println("res: "+res);
    }
    /*
     * 使用对撞指针,
     */
    public int maxPrime(int num){
        int i=num;
        
        while(i>1){
            int m=2,n=i-1;
            while (m<=n) {
                if (m*n==i) {
                    break;
                }
                else if (m*n>i) {
                    n--;
                }
                else {
                    m++;
                }
            }
            if (m>n) {
                return i;
            }
            i--;
        }
        return 1;
    }
    
}

猜你喜欢

转载自blog.csdn.net/wwzheng16/article/details/80979134