在java中不使用数组、数据结构或任何api的情况下因子之间的最短距离?

在java中不使用数组、数据结构或任何api的情况下因子之间的最短距离?,java,Java,我被问到一个问题,在不使用数组、任何数据结构或Java中的字符串的情况下,寻找一个数的因子之间的最短距离。例如,如果将13013传递给一个方法,它应该返回2,因为13013的因子是1*7*11*13*13,两个不同数字之间的最短距离是13-11=2,如果传递了10,它应该返回1,因为因子10是1*2*5,最短距离是2-1,即1。我发现,如果偶数被传递,它应该总是返回1,因为2,1总是包含在因子中,2-1=1,如果素数被传递,它将是1,因为素数只能被自身和1整除,如果奇数被传递,它通常是2,因为3

我被问到一个问题,在不使用数组、任何数据结构或Java中的字符串的情况下,寻找一个数的因子之间的最短距离。例如,如果将13013传递给一个方法,它应该返回2,因为13013的因子是1*7*11*13*13,两个不同数字之间的最短距离是13-11=2,如果传递了10,它应该返回1,因为因子10是1*2*5,最短距离是2-1,即1。我发现,如果偶数被传递,它应该总是返回1,因为2,1总是包含在因子中,2-1=1,如果素数被传递,它将是1,因为素数只能被自身和1整除,如果奇数被传递,它通常是2,因为3-1=2,但我不能找到所有的数

这就是我到目前为止所拥有的,谢谢大家

static int findDistance(int numberPassed)
{   
    boolean isPrime=true;
    int returnValue=0;

    if(numberPassed % 2==0)
    {   
        //If it is even there must be 2 and 1 as a factor therefore 2-1=1
        returnValue=1;
    }
    else
    {
        for(int i=2;i<numberPassed;i++)
        {
            if(numberPassed % i==0)
            {   
                //if number is divisible by any except by 1 and itself
                //it is prime 
                isPrime=false;
                break;
            }
        }
        if(isPrime && numberPassed!=1)
        {   
            //If it is prime then return value should be numberPassed-1
            //since factor of prime are 1 and itself, so shortest distance
            //numberPassed-1
            returnValue=numberPassed-1;
        }
        else
        {   
            //The problem is here if the number is not prime and is odd
            //since shortest distance differes now, for value divisible by 3 
            //it should be 3-1=2, but there are many other cases
            returnValue=2;
        }
    }
    return returnValue;
}
这里,使用了两个for循环:外部for循环用于一次获取一个值,并计算两个值之间的距离。第二个值从内部for循环获得,并存储在距离变量中。由于数字各因子之间的最小距离总是小于其本身,因此MindDistance被初始化为数字本身。如果距离小于minDistance值,则minDistance将重置为新的距离值


那么,实际的问题是什么?我们不是家庭作业或学校作业的垃圾场,我们不会帮助那些不付出任何努力的人,也不会向他们展示问题是一个干净而聪明的代码,它能在因素之间找到最短的距离,谢谢你。@Hendrix333不,这是你想要的,放在盘子里,除了把问题写在盘子里,你不需要付出任何努力。是什么阻止了你自己这么做?我只是在寻找逻辑,我不需要别人给我写代码,谢谢。安迪·特纳先生,就像我在我的帖子中提到的,我发现偶数总是返回1作为最短距离,素数返回数-1,我被困在奇数中,大部分返回2,但并非总是如此。感谢启动agaim drin zur Start不会给您提供更多信息,因此没有必要。虽然此代码可能会回答问题,但提供有关如何和/或为什么解决问题的附加上下文将提高答案的长期价值。虽然此代码可能会解决问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。感谢您的建议!!!我们将研究它,以帮助未来的读者。
static int findDistance(int a)
{   
    //variables needed for calculation 
    int temp=a;
    int fact=1;
    int distance=0;
    int shortestDistance=a;
    int lastFact=1;

    while(temp>1)
    {   
        //If value is divisible by fact variable
        if(temp % fact==0)
        {   
            //If last fact does not equal current fact
            if(lastFact!=fact)
            {
            distance=fact-lastFact;
            lastFact=fact;
            }
            //divide by fact
            temp=temp/fact;

            //reassign the fact to 1 so that it can start from beginning
            fact=1;

            //if distance is not zero and shortestDistance is bigger
            if(shortestDistance>distance && distance !=0)
            {   
                //now the shortest distance is the distance
                shortestDistance=distance;
            }
        }

        fact++;
    }
    return shortestDistance;
}
public static int minDistance(int num){
        int fact1 = 1;
        int minDist = num;
        int fact2 = 0;
        for(int i=2; i<num; i++){
            if (num%i==0){
            fact2 = i;
                if((fact2-fact1)<=minDist){
                    minDist = fact2-fact1;
                    fact1 = fact2;                   
                }           
            }
        }
    return (minDist);
}    
public class Demo {
    public static void main(String[] args) {
        System.out.println("Result: " + minDistance(1001));
    }

    public static int minDistance(int n) {
        int minDistance = n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (n % i == 0 && n % j == 0) {
                    if (i > j) {
                        int distance = i - j;
                        if (distance < minDistance) {
                            minDistance = distance;
                        }
                    }
                }
            }
        }
        return minDistance;
    }
}