Va 160 Factors ans Factorials (number theory)

Source: Internet
Author: User

I haven't written any questions since I switched to linux. Start writing again today.

First, find the number of each prime factor in the class of a number, then we can calculate the number of prime factor for each number starting from 2 to this number, corresponding addition. For example, 10! = 1*2*3*4*5*6*7*8*9*10, the number of 2 is 2, 4, 6, 8, the sum of two factors in 10 is 1 + 2 + 1 + 3 + 1 = 8. And so on

The next step is to find the prime factor and number of each number. The template is enough. Not a difficult question

Second, pay attention to the output format. Here I made a mistake. I did not carefully consider that if the number of outputs is exactly a multiple of 15, then the last line should not be a line feed; when outputting a line feed, note that when k is a multiple of 15, only one line feed is output, because k does not change every cycle value, so if it is written outside of if, there will be a lot of line feed output, not available! Therefore, the processing of this output should be the first when there is a number to be output. If the remainder of 15 k is 1, it means a new line can be entered, then output this number to avoid the case where it is just a multiple of 15!

The core code is as follows:

int anss[101];int main(){prime_sieve();int n;while ( scanf("%d",&n) != EOF && n ) {for ( int i = 2; i <= 100; ++i ) if (!is_prime(i) ) anss[i] = -1; else anss[i] = 0; for ( int i = 2; i <= n; ++i ) {IIV ans;prime_factorize(i, ans);int len = ans.size();for ( int i = 0; i < len; ++i ) anss[ans[i].first]+=ans[i].second;}printf("%3d! =", n);for ( int i = 2, k = 0; i <= n; ++i ) {if ( anss[i] != -1 ) {   k++;   if ( k > 1 && k % 15 == 1 ) printf("\n      ");   printf("%3d", anss[i]);}}printf("\n");}    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.