分解质因子

分解质因子

// 分解质因子
// 2007-07-23
// By rappizit@yahoo.com.cn

#include 
<iostream>
#include 
<math.h>
using namespace std;

bool PrimeFactor (long long n)
{
    
int i, j;
    
bool first_factor = true;

    
if (n < 2)
        
{
        cout 
<< n << " = " << n << endl;
        
return false;
        }

    cout 
<< n << " = ";
    i 
= 2;
    
while (i <= (int) (sqrt ((double) (n))))
        
{
        j 
= 0;
        
while (n % i == 0)
            
{
            n 
/= i;        // 将 n 里面的 i 因子除尽,确保下一个 i 是素数才能整除 n
            j ++;
            }

        
if (j)    {
            
if (! first_factor)
                
{
                cout 
<< " * ";
                }
 else    {
                    first_factor 
= false;
                    }

            cout 
<< i;
            
if (j > 1)
                
{
                cout 
<< " ^ " << j;
                }

            }

        i 
++;
        }

    
if (n != 1)
        
{
        
if (! first_factor)
            
{
            cout 
<< " * ";
            }

        cout 
<< n;
        }

    cout 
<< endl;
    
return (! first_factor);    // 如果 n 是合数就返回 true
}


void main ()
{
    
long long n = 1;
    
while (n)
        
{
        cin 
>> n;
        
if (n)
            
{
            PrimeFactor (n);
            }

        }

}


控制台的输入输出信息:
-4
-4 = -4
1
1 = 1
3
3 = 3
7
7 = 7
15
15 = 3 * 5
31
31 = 31
63
63 = 3 ^ 2 * 7
127
127 = 127
255
255 = 3 * 5 * 17
65535
65535 = 3 * 5 * 17 * 257
4294967295
4294967295 = 3 * 5 * 17 * 257 * 65537
9223372036854775807
9223372036854775807 = 9223372036854775807
0
(退出)

==================================
2007-08-16

修改了一下代码。。
// PrimeFactor 分解质因子
// PrintPrimeFactor 输出分解式,如 100 = 2 ^ 2 * 5 ^ 2

#include 
<iostream>
#include 
<cmath>
#include 
<vector>
using namespace std;

vector 
< pair <intint> > PrimeFactor (int n)
{
    vector 
< pair <intint> > v;
    
if (n < 1)
        
{
        
return v;
        }

    
if (n == 1)
        
{
        v.push_back (pair 
<intint> (11));
        }

    
int i = 2, root = int (sqrt (double (n)));
    
while (i <= root)
        
{
        
if (n % i == 0)
            
{
            
int j = 0;
            
do    {
                n 
/= i;
                j 
++;
                }
 while (n % i == 0);
            v.push_back (pair 
<intint> (i, j));
            root 
= int (sqrt (double (n)));
            }

        i 
++;
        }

    
if (n != 1)
        
{
        v.push_back (pair 
<intint> (n, 1));
        }

    
return v;
}


void PrintPrimeFactor (int n)
{
    cout 
<< n << " = ";
    
if (n < 1)
        
{
        cout 
<< n << endl;
        
return;
        }

    
int i, size;    
    vector 
< pair <intint> > v = PrimeFactor (n);
    size 
= v.size ();
    
for (i = 0; i < size - 1; i ++)
        
{
        cout 
<< v [i].first;
        
if (v [i].second > 1)
            
{
            cout 
<< " ^ " << v [i].second;
            }

        cout 
<< " * ";
        }

    cout 
<< v [i].first;
    
if (v [i].second > 1)
            
{
            cout 
<< " ^ " << v [i].second;
            }

    cout 
<< endl;
    
return;
}


void main ()
{
    
int n;
    cin 
>> n;
    PrintPrimeFactor (n);
    
return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值