The largest prime factor(最大质因数)

1. 问题:

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

2. 解法(by java in Eclipse)

  

 1 package com.lun.alrithmetic;
 2 /*
 3  * Q1: what‘s the primary factor? (1 2 3)
 4  * Q2: i & i+2ne能否遍历出所有质数
 5  */
 6
 7 public class LargestPrimeFactor {
 8
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         long n = 600851475143L;
12         System.out.println("The largest prime factor of the number ‘600851475143‘ is "+largestPrimeFactor(n));
13     }
14
15     public static boolean isPrime(long n){
16         if(n<4)
17             return n>1;
18         if(n%2==0 || n%3==0)
19             return false;
20         for(long i=5; i*i<n; i+=6)
21             if(n%i==0 || n%(i+2)==0)
22                 return false;
23         return true;
24     }
25
26     public static long largestPrimeFactor(long n){
27             if(n%2==0){
28                 while(n%2==0)
29                     n /= 2;
30             }
31             if(n%3==0){
32                 while(n%3==0)
33                     n /= 3;
34             }
35             for(int i=5; i*i<n; i+=6){
36                 if(n%i==0){
37                     while(n%i==0)
38                         n /= i;
39                 }
40                 int j = i+2;
41                 if(n%j==0){
42                     while(n%j==0)
43                         n /= j;
44                 }
45             }
46         return n;
47     }
48 }

way1

 1 package com.lun.alrithmetic;
 2 /*
 3  * Question: What‘s the largest prime factor of the number ‘600851475143‘ ?
 4  * Function: Divide & Conquer, analyse prime factor
 5  */
 6
 7 public class LargestPrimeFactor2 {
 8
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         long n = 600851475143L;
12         System.out.println("The largest prime factor of the number ‘600851475143‘ is "+largest(n));
13     }
14
15     public static boolean isPrime(long n){
16         if(n<4)
17             return n>1;
18         if(n%2==0 || n%3==0)
19             return false;
20         for(long i=5; i*i<n; i+=6)
21             if(n%i==0 || n%(i+2)==0)
22                 return false;
23         return true;
24     }
25
26     public static long largest(long n){
27         if(isPrime(n))
28             return n;
29         else{
30             if(n%2==0)
31                 return largest(n/2);
32             if(n%3==0)
33                 return largest(n/3);
34             for(long i=5; i*i<n; i+=6){
35                 if(n%i==0)
36                     return largest(n/i);
37                 if(n%(i+2)==0)
38                     return largest(n/(i+2));
39             }
40         }
41
42         return 1;
43     }
44
45 }

way2

注:利用分解质因数的方法,从小向大用质数整除(如果此质数恰好是n的因数的话)n,即不断的减小n的规模,最后即可求的最大质因数。

法二,采用了递归的方法易于理解,但是内存消耗较大;建议用法一。

时间: 2024-12-11 03:27:01

The largest prime factor(最大质因数)的相关文章

Largest prime factor

problem 3:Largest prime factor 题意:求600851475143的最大的质因数 代码如下: 1 #ifndef PRO3_H_INCLUDED 2 #define PRO3_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro3{ 7 long long solve(){ 8 long long n=600851475143LL,maxn=0;; 9 for(long long i=1;i*i<=n;

(hdu step 2.1.3)Largest prime factor(求一个数的最大质因子的位置)

题目: Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4868 Accepted Submission(s): 1452   Problem Description Everybody knows any number can be combined by the prime number.Now,

HDU 2136 Largest prime factor 数论

Largest prime factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Everybody knows any number can be combined by the prime number. Now, your task is telling me what position of the largest prime factor. The

2136 Largest prime factor(打表)

Problem Description Everybody knows any number can be combined by the prime number.Now, your task is telling me what position of the largest prime factor.The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.Specially, LPF(1) = 0. Input E

hdu 2136 (Largest prime factor)就是简单 的筛选素数法

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7009    Accepted Submission(s): 2482 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU 2136 Largest prime factor (筛选法求素数)

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7297    Accepted Submission(s): 2589 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10004    Accepted Submission(s): 3534 Problem Description Everybody knows any number can be combined by the prime number. No

HDU 2136 Largest prime factor

水题,晒一遍素数并标注就OK了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<

HDU 2136 Largest prime factor (素数打表。。。)

题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring>