[英]Finding all the factors of a number in Java?
"Write a program that reads an integer I and displays all its smallest factors in increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.". “编写一个程序,读取一个整数 I 并按升序显示其所有最小因数。例如,如果输入整数为 120,则输出应如下所示:2, 2, 2, 3, 5。”。 At the beginning of the program, the user has to enter an integer identifying how many numbers will be factorized.在程序开始时,用户必须输入一个整数,标识将被因式分解的数字的数量。
import java.util.Scanner;
public class Main {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int size = input.nextInt();
for(int i = 0; i < size; i++){
int a = input.nextInt();
for(int j = 0; j < a; j++){
if(a%j==0){
System.out.println(j);
}
}
}
input.close();
}
}
A Better way of finding all the factors is to find the factors till it's square root.找到所有因子的更好方法是找到因子直到它的平方根。
int n = 120;
for(int i = 2; i * i <= n; ++i)//check below it's square root i <= sqrt(n)
if(n % i == 0){
while(n % i == 0)
{
System.out.println(i);
n /= i;
}
}
A much more effective way is to do it with primes.一个更有效的方法是用素数来做。
There cannot be any other prime factor which is even other than 2
so we can skip the even part不可能有比甚至其他任何其他质因数2
,所以我们可以跳过偶数部分
int n = 120;
if(n % 2 == 0)
{
while(n % 2 == 0)
{
System.out.println("2");
n /= 2;
}
}
for(int i = 3; i * i <= n; i += 2)//odd numbers only
{
while(n % i == 0)
{
n /= i;
System.out.println(i);
}
}
A much more efficient way is to use 6*k +- 1 rule,更有效的方法是使用 6*k +- 1 规则,
What is 6*k +- 1 rule?什么是 6*k +- 1 规则?
All prime numbers(except 2 and 3) can be represented by the above formula.所有素数(2和3除外)都可以用上面的公式表示。 Though the reverse might not be true, Consider 6*6 - 1 = 35 divisible by 5.虽然反过来可能不是真的,但请考虑 6*6 - 1 = 35 可被 5 整除。
If it is not a prime, it will have a prime factor less than it's square root.如果它不是素数,它的素数因子将小于它的平方根。
So we check only for the numbers which follow the above rule.所以我们只检查符合上述规则的数字。
int i = 1, n = 120;
//check for 2 and 3
if(n % 2 == 0)
{
while(n % 2 == 0)
{
System.out.println("2");
n /= 2;
}
}
if(n % 3 == 0)
{
while(n % 3 == 0)
{
System.out.println("3");
n /= 3;
}
}
while(true)
{
int p = 6 * i - 1;
if(p * p > n)
break;
if(n % p == 0)
{
while( n % p == 0)
{
n /= i;
System.out.println(p);
}
}
p = 6 * k + 1;
if(p * p > n)
break;
if(n % p == 0)
{
while( n % p == 0)
{
n /= i;
System.out.println(p);
}
}
}
If the numbers are very huge and there are alot of them, Pre-calculate primes can be helpful如果数字非常大并且数量很多,则预先计算素数可能会有所帮助
I use Sieve to calculate the primes.我使用 Sieve 来计算素数。
int max = 10000007;
boolean[]primes = new boolean[max];
int []nums = new int[max];
int numOfPrimes = 0;
for(int i = 2; i * i < max; ++i)
if(!primes[i])
for(int j = i * i; j < max; j += i)//sieve
primes[j] = true;
for(int i = 2; i < max; ++i)
if(!primes[i])
nums[numOfPrimes++] = i;//we have all the primes now.
int n = 120;
for(int i = 0; i < numOfPrimes; ++i)
{
int p = nums[i];
if(p * p > n)
break;
if(n % p == 0)
{
while(n % p == 0)
{
n /= p;
System.out.println(p);
}
}
}
You should divide the number:你应该除以这个数字:
for(int j = 2; j < a; j++){ // start dividing from 2
if(a%j==0){
System.out.println(j);
a/=j; // divide a with j (there is remainder 0 because of condition)
j--; // do j once more
}
}
Try this one:试试这个:
package bölüm05;
import java.util.Scanner;
public class B05S16 {
public static void main(String[] args) {
Scanner java = new Scanner(System.in);
System.out.println("Bir tamsayı giriniz");
int sayı = java.nextInt();
int i = 2;
while (sayı > 1) {
if (sayı % i == 0) {
sayı = sayı / i;
System.out.print(i + ",");
} else {
i++;
}
}
java.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.