用C语言实现,将100以内的自然数分解质因数
使用C语言,编写代码,实现100以内的自然数分解质因数
------解决方案--------------------
#include
#include
int fenjie(int n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
printf("%d*",i);
return fenjie(n/=i);
break;
}
}
return printf("%d\n",n);
}
int main()
{
int num;
while(scanf("%d",&num)!=EOF)
fenjie(num);
return 0;
}
------解决方案--------------------
任意输入一个整数,将这个整数进行质因数分解n(2<=n<=100000000);
------解决方案--------------------
Finding prime numbers - Kenneth Haugland
Different schemas for finding prime numbers explained with code
http://www.codeproject.com/Article.aspx?tag=198374988322054872&
------解决方案--------------------
仅供参考,尽管是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