c语言100以内分解质因数,用C语言实现,将100以内的自然数分解质因数

仅供参考,尽管是C#

//******************************************************************************

// Author           :   Alexander Bell

// Copyright        :   2007-2011 Infosoft International Inc

// Date Created     :   01/15/2007

// Last Modified    :   02/08/2011

// Description      :   Prime Factoring

//******************************************************************************

// DISCLAIMER: This Application is provide on AS IS basis without any warranty

//******************************************************************************

//******************************************************************************

// TERMS OF USE     :   This module is copyrighted.

//                  :   You can use it at your sole risk provided that you keep

//                  :   the original copyright note.

//******************************************************************************

using System;

using System.Collections.Generic;

namespace Infosoft.MathShared

{

/// Integers: Properties and Operations

public  static partial class Integers

{

#region Prime Numbers <100

private static readonly int[] Primes =

new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23,

29, 31, 37, 41, 43, 47, 53, 59,

61, 67, 71, 73, 79, 83, 89, 97 };

#endregion

// starting number for iterative factorization

private const int _startNum = 101;

#region IsPrime: primality Check

/// 

/// Check if the number is Prime

/// 

/// Int64

/// bool

public static bool IsPrime(Int64 Num){

int j;

bool ret;

Int64 _upMargin = (Int64)Math.Sqrt(Num) + 1; ;

// Check if number is in Prime Array

for (int i = 0; i 

if (Num == Primes[i]) { return true; }

}

// Check divisibility w/Prime Array

for (int i = 0; i 

if (Num % Primes[i] == 0) return false;

}

// Main iteration for Primality check

_upMargin = (Int64)Math.Sqrt(Num) + 1;

j = _startNum;

ret = true;

while (j <= _upMargin)

{

if (Num % j == 0) { ret = false; break; }

else { j++; j++; }

}

return ret;

}

/// 

/// Check if number-string is Prime

/// 

/// string

/// bool

public static bool IsPrime(string StringNum) {

return IsPrime(Int64.Parse(StringNum));

}

#endregion

#region Fast Factorization

/// 

/// Factorize string converted to long integers

/// 

/// string

/// Int64[]

public static Int64[] FactorizeFast(string StringNum) {

return FactorizeFast(Int64.Parse(StringNum));

}

/// 

/// Factorize long integers: speed optimized

/// 

/// Int64

/// Int64[]

public static Int64[] FactorizeFast(Int64 Num)

{

#region vars

// list of Factors

List _arrFactors = new List();

// temp variable

Int64 _num = Num;

#endregion

#region Check if the number is Prime (<100)

for (int k = 0; k 

{

if (_num == Primes[k])

{

_arrFactors.Add(Primes[k]);

return _arrFactors.ToArray();

}

}

#endregion

#region Try to factorize using Primes Array

for (int k = 0; k 

{

int m = Primes[k];

if (_num 

while (_num % m == 0)

{

_arrFactors.Add(m);

_num = (Int64)_num / m;

}

}

if (_num 

{

_arrFactors.Sort();

return _arrFactors.ToArray();

}

#endregion

#region Main Factorization Algorithm

Int64 _upMargin = (Int64)Math.Sqrt(_num) + 1;

Int64 i = _startNum;

while (i <= _upMargin)

{

if (_num % i == 0)

{

_arrFactors.Add(i);

_num = _num / i;

_upMargin = (Int64)Math.Sqrt(_num) + 1;

i = _startNum;

}

else { i++; i++; }

}

_arrFactors.Add(_num);

_arrFactors.Sort();

return _arrFactors.ToArray();

#endregion

}

#endregion

}

}

//******************************************************************************

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值