More efficient way of calculating prime factorisation?

platelminto

I currently have a program to find the prime factorisation of a given number; works fine with smaller numbers, but it takes ages for anything over a million. My code is extremely inefficient, finding all prime numbers below the input and checking which ones divide without a remainder. I don't know how to make it less inefficient, any help?

static ArrayList<Integer> primeNumbersBelow(long n) {

    ArrayList<Integer> ay = new ArrayList<Integer>();
    ay.add(2);

    for(int i = 3; i < ((n % 2 != 0) ? (n + 1) / 2 : n / 2); i++) {
        boolean divides = false;
        for(int j = 2; j < i; j++) {
            if(i % j == 0) {
                divides = true;
            }
        }
        if(!divides) {
            ay.add(i);
            System.out.println(i);
        }
    }
    return ay;
}

static ArrayList<Integer> primeFactorisationOf() {

    ArrayList<Integer> ay = new ArrayList<Integer>();
    ArrayList<Integer> aay = primeNumbersBelow(input);
    long n = input;

    for(int i = 0, len = aay.size(); i < len; i++) {
        int f = aay.get(i);
        boolean run = true;

        while(run) {
            if(n % f == 0) {
                ay.add(f);
                n /= f;
            } else {
                run = false;
            }
        }
    }
    return ay;
}
Jordi Castilla

From Mr Lars Vogel @ vogella...

 public static List<Integer> primeFactors(int number) {
    int n = number;
    List<Integer> factors = new ArrayList<Integer>();
    for (int i = 2; i <= n; i++) {
      while (n % i == 0) {
        factors.add(i);
        n /= i;
      }
    }
    return factors;
  }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Efficient way of calculating quadratic forms: avoid for loops?

More efficient way to write this algorithm?

Is there more efficient way to write this SQL?

Faster / More efficient way of binding all?

Is there a more efficient way to handle string escaping in this function?

More efficient way to compare numbers than ifelse?

JQuery Table Sorting - More Efficient way?

More efficient way to call vector.size()

What is this sorting algorithm? And is there a more efficient way?

More efficient way to fetch data from MySQL

Calculating and printing the nth prime number

Repeated CASE Statement: Is there a more efficient way of writing this SQL statement?

Is "$onInit", more efficient way than "activate" to activate "controller" in angularJS?

Scala: More Efficient Way to Filter a List and Create a Sequence of Futures

How can I write the following code in a more efficient and pythonic way?

Is there a more efficient way to return the record of maximum spatial intersection in SQL?

Is there a more efficient way to sort a list than list.sort() in Python?

More efficient way to delete list item with index Python

JavaScript: it works, but what is a more efficient way to write this function?

More efficient way to iterate on my data (DbReader/DataSet)

Could this be more efficient in Go?

Why is GRO more efficient?

More efficient sort algorithm?

Is there a shorter/more efficient way to use the spread operator in javascript to update a key's value?

More efficient way to run a random walk on a transition matrix than the igraph::random_walk()

Which is the more efficient way to choose a random pair of objects from a list of lists or tuples?

Which is the more efficient way to choose a random pair of objects from a list of lists or tuples?

More efficient way to append a list item after some text in Google Docs

What is a more efficient and collaborate way of merging branches that has too many commits?

TOP 一覧

  1. 1

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    CSSのみを使用して三角形のアニメーションを作成する方法

  4. 4

    ドロップダウンリストで選択したアイテムのQComboBoxスタイル

  5. 5

    ZScalerと証明書の問題により、Dockerを使用できません

  6. 6

    PyCharmリモートインタープリターはプロジェクトタブにサイトパッケージのコンテンツを表示しません

  7. 7

    Windows 10でのUSB入力デバイスの挿入/取り外しの検出

  8. 8

    Excel - count multiple words per cell in a range of cells

  9. 9

    PictureBoxで画像のブレンドを無効にする

  10. 10

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  11. 11

    スタート画面にシャットダウンタイルを追加するにはどうすればよいですか?

  12. 12

    Python / SciPyのピーク検出アルゴリズム

  13. 13

    Luaの文字列から特定の特殊文字を削除するにはどうすればよいですか?

  14. 14

    Pythonを使用して、リストからデータを読み取り、特定の値をElasticsearchにインデックス付けするにはどうすればよいですか?

  15. 15

    LinuxでPySide2(Qt for Python)をインストールするQt Designerはどこにありますか?

  16. 16

    goormIDEは、ターミナルがロードするデフォルトプロジェクトを変更します

  17. 17

    QGISとPostGIS(マップポイント(米国の地図上にraduisを使用した緯度と経度)

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    ターミナルから「入力ソースの変更」ショートカットを設定する

  20. 20

    パンダは異なる名前の列に追加します

  21. 21

    同じクラスの異なるバージョンを使用したクラスローディング:java.lang.LinkageError:名前の重複クラス定義を試行しました

ホットタグ

アーカイブ