欧拉计划21-23题

21、Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a  b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

题目大意:

d(n)定义为n 的所有真因子(小于 n 且能整除 n 的整数)之和。 如果 d(a) = b 并且 d(b) = a, 且 a  b, 那么 a 和 b 就是一对相亲数(amicable pair),并且 a 和 b 都叫做亲和数(amicable number)。

例如220的真因子是 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 和 110; 因此 d(220) = 284. 284的真因子是1, 2, 4, 71 和142; 所以d(284) = 220.

计算10000以下所有亲和数之和。

#include<stdio.h>



int FactorSum(int n)  //计算n的所有小于n的因素和

{

    int i;

    int sum=1;

    for(i=2; i<=n/2; i++)

    {

        if(n%i==0)

            sum+=i;

    }

    return sum;

}



int main()

{

    int t,i=2;

    int sum=0;

    while(i<10000)

    {

        t=FactorSum(i);

        if(t!=i && FactorSum(t)==i) 

            sum+=i;

        i++;

    }

    printf("%d\n",sum);

    return 0;

}
View Code
 
Answer:31626
Completed on Wed, 24 Jul 2013, 06:07

22、 Names scores

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938  53 = 49714.

What is the total of all the name scores in the file?

题目大意:

文件names.txt (右键另存为)是一个46K大小的文本文件,包含5000多个英文名字。利用这个文件,首先将文件中的名字按照字母排序,然后计算每个名字的字母值,最后将字母值与这个名字在名字列表中的位置相乘,得到这个名字的得分。

例如将名字列表按照字母排序后, COLIN这个名字是列表中的第938个,它的字母值是3 + 15 + 12 + 9 + 14 = 53。所以COLIN这个名字的得分就是938  53 = 49714.

文件中所有名字的得分总和是多少?

#include <stdio.h> 

#include <ctype.h>

#include <stdlib.h>



struct str{

    char name[12];

};



char a[12];



int namevalue(char *s)

{

    int i, sum;

    i = sum = 0;

    while(s[i]) {

        sum += (s[i] - 'A' + 1);

        i++;

    }

    return sum;

}



int cmp(const void *a, const void *b)

{

    return strcmp((*(struct str*)a).name, (*(struct str*)b).name);

}



void solve(void)

{

    FILE *fp;

    int i, j, k;

    char *s, c;

    long long sum = 0;



    struct str namestring[5163];



    fp = fopen("names.txt", "r");

    fseek(fp, 0, SEEK_END);

    int file_size;

    file_size = ftell(fp);

    fseek(fp, 0, SEEK_SET);

    s = (char*)malloc(file_size * sizeof(char));

    fread(s, sizeof(char), file_size, fp);



    i = j = k = 0;

    while(i <= file_size) {

        c = s[i++];

        if(!isalpha(c)) {

            if(c == ',') {

                j = 0;

                strcpy(namestring[k++].name, a);

                memset(a,'\0',12 * sizeof(char));

            }

        } else {

            a[j++] = c;

        }

    }

    strcpy(namestring[k].name, a);

    memset(a,'\0',12 * sizeof(char));



    qsort(namestring, 5163, sizeof(namestring[0]),cmp);



    for(i = 0; i <= 5162; i++) {

        sum += (namevalue(namestring[i].name) * (i + 1));

    }

    printf("%lld\n",sum);

}



int main(void)

{

    solve();    

    return 0;

}
View Code
 
Answer:871198282
Completed on Mon, 18 Nov 2013, 15:03
 

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

题目大意:

如果一个数的所有真因子之和等于这个数,那么这个数被称为完全数。例如,28的所有真因子之和为1 + 2 + 4 + 7 + 14 = 28,所以28是一个完全数。

如果一个数的所有真因子之和小于这个数,称其为不足数,如果大于这个数,称其为过剩数。

12是最小的过剩数,1 + 2 + 3 + 4 + 6 = 16。因此最小的能够写成两个过剩数之和的数字是24。经过分析,可以证明所有大于28123的数字都可以被写成两个过剩数之和。但是这个上界并不能被进一步缩小,即使我们知道最大的不能表示为两个过剩数之和的数字要比这个上界小。

找出所有不能表示为两个过剩数之和的正整数之和。
#include <iostream>

using namespace std;

int sum(int x){

    int xx = x / 2, sum = 0;

    for (int i = 1; i <= xx; i++)

        (x % i == 0) ? sum += i : sum = sum;

    return sum;

}

void main(){

    int b[28124] = { 0 }, c[28124] = { 0 }, x = 0;

    for (int i = 2; i <= 28123; i++){

        x = sum(i);

        if (x > i) 

            b[i] = 1;

    }

    for (int i = 2; i <= 28123; i++)

        for (int j = 2; j <= 28123 - i; j++)

            if (c[i + j] != 2)

            c[i + j] = b[i] + b[j];

    for (int i = 2; i <= 28123; i++)

        if (c[i] != 2) x += i;

    cout << x << endl << endl;

}
View Code
Answer:4179871
Completed on Sun, 13 Apr 2014, 08:23
 
 

你可能感兴趣的:(欧拉计划)