2017-11-30 2 views
0

リアルタイムで4分の1秒を要しないにもかかわらず、このプロセスが終了するまでに最大5秒かかります。ストップウォッチが正確な時刻を報告していません

ストップウォッチに特に関連するコードを太字にして、あなたがそれをすべて見る必要がないようにします。これは初めての投稿なので親切にしてください。不器用な場合はごめんなさい。コードが太字になっていない場合は、問題の部分の周りに**が表示されます。

*バックグラウンド:これは数学のエッセイです。それは素因を見つけ、それを見つけるのにどれくらい時間がかかるかを示すプログラムでなければなりません。それは素因を見つけることに取り組んでいるが、ストップウォッチは数秒でばかげた数字を報告している。また、このコードは、最も頻繁にストップウォッチ、ユーザの入力機能、および反復と

http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/

の影響を受けているが、私自身の考えによって、または他人の助けを借り*

// Program to print all prime factors 
import java.io.*; 
import java.lang.Math; 
import java.util.Scanner; 
import java.text.DecimalFormat; 

class primeFactorer4 
{ 
    **static long startTime = System.nanoTime();** 
    // A function to print all prime factors 
    // of a given number n 
    public static void primeFactors(long n) 
    { 
     // Print the number of 2s that divide n 
     while (n%2==0) 
     { 
      System.out.print(2 + " "); 
      n /= 2; 
     } 

     // n must be odd at this point. So we can 
     // skip one element (Note i = i +2) 
     for (int i = 3; i <= Math.sqrt(n); i+= 2) 
     { 
      // While i divides n, print i and divide n 
      while (n%i == 0) 
      { 
       System.out.print(i + " "); 
       n /= i; 
      } 
     } 

     // This condition is to handle the case whien 
     // n is a prime number greater than 2 
     if (n > 2) 
      System.out.print(n); 
    } 

    public static void main (String[] args) 
    { 
     Console console = System.console(); 
     String input = console.readLine("Enter input:"); 
     long n = Long.valueOf(input); 
     for (int k=1; k<=10; k++) 
    { 
     primeFactors(n); 
     System.out.println(" Try " + k); 
    } 
     **double endTime = System.nanoTime(); 
     double totalTime = endTime - startTime; 
     DecimalFormat totalTimeFormat = new DecimalFormat("##.###"); 
     System.out.println(" Time taken in seconds:" + totalTimeFormat.format(totalTime/10/1000000000));** 
     primeFactorer4.main(args); 
    //reason for the weird division is for clarity. "totalTime" is the time surpassed 
    //to repeat all the methods, the "10" in the middle is to get the mean total time 
    //of all the primeFactors cycles, and the "1000000000" at the end is to convert nanoseconds into seconds 
    } 
} 

理由でいずれかに添加されています私がprimeFactorsに10回電話をかけたのは、私のコンピュータが私のために結果の平均を出したかったからです。なぜなら、どの学校からも実験をすると、より正確なIVレベル3(またはそれ以上)結果

+0

あなたはいつもメソッドを 'for'ループに張り、コードをきれいにすることができます。 – UghThatGuyAgain

+0

コンピュータの数学は科学的な実験ではありません。それを10回呼び出すのは無意味です。 –

+0

これはすべての実験ではありません。私はこの結果を、数の大きさと素因数を見つけるための時間とに関連させたグラフに入れていました。そして、私の先生が教えてくれた回帰直線を見つけることは、それを数学に関連させるために最低限必要でした。私の主題は主な要素であり、エッセイとして興味深い主要因を暗号化について論じるときに私が知っている唯一の方法です。 – regazzo

答えて

0

私はあなたのために何かを書いた、私はこれが速く、したがってより正確かもしれないと思うが、正確にすることができないようにANYステートメントを実行する時間がかかることを覚えておいてください。

long start= System.currentTimeMillis(); //start time 
//Insert code here 
long difference = System.currentTimeMillis(); //finish time 
difference -= start; 
System.out.println("Time took to run code was " + difference);; //Print the amount of time that it took 
+0

おかげでブレンナン、あなたのものはもっと複雑で、私よりもはるかにきれいに見えますが、私の解決策が見つかりました。 – regazzo

1

私は自分の問題を解決しました。私はprintlnコマンドをstartTime変数とendTime変数の下に置いていました。プログラムが開始されたときにstartTime変数が開始していたことがわかりました。私の個人的な数字の入力速度には関係ないと感じる適切な結果が得られました。

プログラムに興味のある人は、この問題の解決策があなたに当てはまるか、解決策と問題の対比に興味があるだけです。新しいコードはここにあります。

// Program to print all prime factors 
import java.io.*; 
import java.lang.Math; 
import java.util.Scanner; 
import java.text.DecimalFormat; 

class primeFactorer4 
{ 

    // A function to print all prime factors 
    // of a given number n 
    public static void primeFactors(long n) 
     { 
      // Print the number of 2s that divide n 
      while (n%2==0) 
      { 
       System.out.print(2 + " "); 
       n /= 2; 
      } 

      // n must be odd at this point. So we can 
      // skip one element (Note i = i +2) 
      for (int i = 3; i <= Math.sqrt(n); i+= 2) 
      { 
       // While i divides n, print i and divide n 
       while (n%i == 0) 
       { 
        System.out.print(i + " "); 
        n /= i; 
       } 
      } 

      // This condition is to handle the case whien 
      // n is a prime number greater than 2 
      if (n > 2) 
       System.out.print(n); 
     } 



    public static void main (String[] args) 
    { 
     Console console = System.console(); 
     String input = console.readLine("Enter input:"); 
     long n = Long.valueOf(input); 
     long startTime = System.nanoTime(); 
     System.out.println(startTime); 
     for (int k=1; k<=10; k++) 
     { 
      primeFactors(n); 
      System.out.println(" Try " + k); 
     } 
     double endTime = System.nanoTime(); 
      System.out.println(endTime); 
      double totalTime = endTime - startTime; 
      DecimalFormat totalTimeFormat = new DecimalFormat("##.##########"); 
      System.out.println(" Time taken in seconds:" + totalTimeFormat.format(totalTime/10/1000000000)); 
     primeFactorer4.main(args); 
    //reason for the weird division is for clarity. "totalTime" is the time surpassed 
    //to repeat all the methods, the "10" in the middle is to get the mean total time 
    //of all the primeFactors cycles, and the "1e9" at the end is to convert nanoseconds into seconds 
    } 
} 
関連する問題