UVa OJ 160 Factors and Factorials



 Factors and Factorials 

The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. It is often defined recursively as follows:

displaymath27

displaymath28

Factorials grow very rapidly--5! = 120, 10! = 3,628,800. One way of specifying such large numbers is by specifying the number of times each prime number occurs in it, thus 825 could be specified as (0 1 2 0 1) meaning no twos, 1 three, 2 fives, no sevens and 1 eleven.

Write a program that will read in a number N ( tex2html_wrap_inline39 ) and write out its factorial in terms of the numbers of the primes it contains.

Input

Input will consist of a series of lines, each line containing a single integer N. The file will be terminated by a line consisting of a single 0.

Output

Output will consist of a series of blocks of lines, one block for each line of the input. Each block will start with the number N, right justified in a field of width 3, and the characters `!', space, and `='. This will be followed by a list of the number of times each prime number occurs in N!.

These should be right justified in fields of width 3 and each line (except the last of a block, which may be shorter) should contain fifteen numbers. Any lines after the first should be indented. Follow the layout of the example shown below exactly.

Sample input

5
53
0

Sample output

  5! =  3  1  1
 53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
        1

#include <cstdio>
#include <cstring>

// 素数判定 
int is_prime(int n) 
{
    for(int i = 2; i*i <= n; ++i)
    {
        if(n % i == 0) 
            return 0;
    }
    return 1;
}

// 素数表 
int prime[100], count = 0;

int main() 
{
    int n, p[100];  // n和各个素数的指数 
    
    // 构造素数表 
    for(int i = 2; i <= 100; i++)
    {
        if(is_prime(i)) 
            prime[count++] = i;
    }
    
    while(scanf("%d", &n) == 1 && n > 0) 
    {
        printf("%3d! =", n);
        memset(p, 0, sizeof(p));
        int maxp = 0;
        for(int i = 1; i <= n; ++i) 
        {
            int m = i;    // 必须把i复制到变量i中,而不是在做除法时直接修改它 
            for(int j = 0; j < count; ++j)
            { 
                while(m % prime[j] == 0) 
                {
                    m /= prime[j];
                    ++p[j];
                    if(j > maxp) 
                    {
                        maxp = j;
                    }
                }
            }
        }
        int count = 0;
        for(int i = 0; i <= maxp; ++i) 
        {
            if(count > 0 && count % 15 == 0) 
            {
                printf("\n      ");
            }
            printf("%3d", p[i]);
            ++count;
        }
        printf("\n");
    }
    
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值