0

だから私はこの問題を抱えています.15としましょう. そして、私が抱えている別の問題は、入力された数値が素数であるかどうかを見つけることです。これを修正する方法はありますか?私はそれが必要で、その後編集する他のものがあります。

public class PrimeFactor 
{
    public static void main(String[] args) 
    {
        Scanner input= new Scanner(System.in);
        int a;
        int d;
        int remainder=0;
        int count=2;
        int c=0;
        String s;
        System.out.println("Enter an integer to be factored:");
        a=input.nextInt();
        s="";
        d=a;
        while(a>1)
        {
            if(a>1)
            { 
                s="";
                while(a>1)
                {
                    remainder=a%count;
                    if (!(remainder>0))
                        while(remainder==0)
                        {
                            remainder=a%count;
                            if (remainder==0)
                            {    
                                a=a/count;
                                c=c+1;
                                s=s+count+"x";
                                if (a==1)
                                    s=s+count;
                            }
                            else
                                count++;
                        }
                    else 
                        count++;
                }
                if (a%count==0)
                {
                    System.out.println(d +"=" + s);
                    System.out.println(d+" is a prime number.");
                }
                else
                    System.out.println(d +"=" + s);
            }
        // TODO code application logic here
        }
    }
}    
4

5 に答える 5

0

これらは、数を因数分解して素数かどうかを判断するために使用できる非常に簡単な方法です。

public static int oneFactor(int i) {
    for (int j = 2; j < i; j++) {
        if (i % j == 0)
            return j;
    }
    return -1;
}

public static Integer[] primeFactors(int i) {
    List<Integer> factors = new ArrayList<Integer>();

    boolean cont = true;
    while (cont) {
        int f = oneFactor(i);
        if (i > 1 && f != -1) {
            i /= f;
            factors.add(f);
        } else
            factors.add(i);
        if (f == -1)
            cont = false;
    }

    return factors.toArray(new Integer[factors.size()]);
}

public static boolean isPrime(int i) {
    if (i == 2 || i == 3)
        return true;

    if (i < 2 || i % 2 == 0)
        return false;

    for (int j = 3, end = (int) Math.sqrt(i); j <= end; j += 2) {
        if (i % j == 0) {
            return false;
        }
    }
    return true;
}

より高速なアルゴリズムを使用できると確信していますが、それらは単純さを犠牲にしており、高速な方法は必要ないようです。それらはすべて int で動作しますが、long で動作するように変更するのは簡単です。
ご不明な点がございましたら、お気軽にお問い合わせください。

于 2013-09-22T18:29:02.980 に答える
0

これにより、数値が素数であるかどうかが最も速い方法であるかどうかが決まります。別の方法として、a を使用しfor loopてその数の因数の数を決定し、2 つ以上の因数がある場合に素数であると言う方法があります。

int num; // is the number being tested for if it's prime.
boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(num); i++) // only have to test until the square root of the number
{
    if (num%i == 0) // if the number is divisible by anything from 2 - the square root of the number
    {
        isPrime = false; // it is not prime
        break; // break out of the loop because it's not prime and no more testing needed
    }
}

if (isPrime) 
{
    System.out.println(num + " is a prime number.");
}
else
{
    System.out.println(num + " is a composite number.");
}
于 2013-09-22T18:29:30.510 に答える
0

因数分解文字列を正しく構築していません。

  • 3 割ると a=15 になることがわかったら、s を商に3x設定します。aa=5
  • 5x5 で割り切れる a=5 が に追加されることがわかったらs、今s3x5xです。次にa、商を 1 に設定します。商が になったので、1再び 5 を追加すると、 が得られ3x5x5ます。

あなたがしたいのは、 ではなくの5場合にのみ追加することです。これを変更する必要があります:a=15x5

s=s+count+"x";
if (a==1)
    s=s+count;

これに:

if (a==1) {
    s=s+count;
} else {
    s=s+count+"x";
}
于 2013-09-22T18:34:16.243 に答える
0

このように試してみてはどうですか:-

for(int i = input-1; i > 0; i--) {
    if((input % i) == 0) {
            if(i == 1)
                System.out.println("Number is a prime");
            else
                System.out.println("Number is not a prime");
            break;
    }       
 }
于 2013-09-22T18:35:42.547 に答える