Snippets

Burdzi0 Faktoryzacja liczby naturalnej za pomocą sita

Created by Burdzi0
package faktoryzacja;

import java.util.ArrayList;
import java.util.List;

import sito.Eratosthenes;

public class Factorization {

	public List<Integer> factorize(int n) {
		Eratosthenes e = new Eratosthenes(n);
		ArrayList<Integer> factorials = new ArrayList<>();
		
		int[] sieve = e.getSieveResult();

		while (n != 1) {
			for (int i = 0; i < sieve.length; i++) {
				while(n % sieve[i] == 0) {
					factorials.add(sieve[i]);
					n /= sieve[i];
				}
			}
		}
		return factorials;
	}
	
	public static void main(String[] args) {
		Factorization f = new Factorization();
		for (int i: f.factorize(15485853)) {
			System.out.println(i);	
		}
	}
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.