project euler 解题

本文探讨了多个Project Euler的数学和编程问题,包括质数分解、回文数、求解最小公约数、计算质数之和、寻找特定性质的数等。涉及动态规划、数论和算法优化,如使用bitset、动态规划和缓存技术来提高效率。
摘要由CSDN通过智能技术生成

project euler 3:Any integer greater than 1 is either a prime number, or can be written as a unique product of prime numbers (ignoring the order).

每一个数都能表示成质数的乘积。

const long numm = 600851475143;
long newnumm = numm;
long largestFact = 0;
 
int counter = 2;
while (counter * counter < newnumm) {
    if (newnumm % counter == 0) {
        newnumm = newnumm / counter;
        largestFact = counter;
    } else {
        counter++;
    }
}
if (newnumm > largestFact) { // the remainder is a prime number
    largestFact = newnumm;
}


project euler 4 (回文即反转之后数仍不变)


project euler 5(计算能整除1到20的最小的数,可以用gcd求出最小公约书,然后从2到20开始计算,出去公约数)。


project euler 10(计算<N的质数的和

我觉得这个方法不错,用bitset,先每位置1,假设全为质数,然后一个一个的置0,以质数的2倍开始置0,以该质数递增。如4,6,8置0,然后6,9置0,一直循环置0,直到其他为1的全为质数。

#include [lt]iostream[gt]
#include [lt]bitset[gt]

using namespace std;

int main()
{
   bitset[lt]1000000[gt] Sieve;
   __int64 sum = 0;
        
   Sieve.flip();      // Set all bits to 1
   Sieve[0].flip();   // Set 0 and 1 to not prime
   Sieve[1].flip();

   // Check all nos from 2 to 1 million
   for(long i = 2; i < 1000000; ++i)
   {
      if( ! Sieve[i] )   // If marked not prime
         continue;      // return to head of loop
      else
         // Set all multiples as not prime
         for(long j = 2*i; j < 1000000; j += i)
            Sieve[j] = 0; 
   }

   for(long i = 2; i < 1000000; ++i)
      if( Sieve[i] )   // Add all nos with bit set
         sum += i;

   cout << "\nThe sum is : "  << sum << endl;

   return 0;
}



project euler 12(求第一个拥有500个因子的triangle number)

每个数都能表示为N个质数的乘积。

求因子数

private int NumberOfDivisors(int number) {
    int nod = 0;
    int sqrt = (int) Math.Sqrt(number);
 
    for(int i = 1; i<= sqrt; i++){
        if(number % i == 0){
            nod += 2;
        }
    }
    //Correction if the number is a perfect square
    if (sqrt * sqrt == number) {
        nod--;
    }
 
    return nod;
}

Any number N is uniquely described by its prime factorisation

\displaystyle N = p_1^{a_1} \cdot p_2^{a_2} \cdot p_3^{a_3}\dots = \prod_{n=1}^K p_n^{a_n}

Where pn is the a distinct prime number, an is the exponent of the prime and K is the set of all prime numbers less than or equal to the square root of N.  Taking any unique combination of the prime factors and multiplying them, will yield a unique divisor of N. That means we can use combinatorics to determine the number of divisors based on the prime factorisation. The  prime pncan be chosen 0,1,…,an times. That means in all we can choose pn in an+1 different ways.

The total number of divisors of N , D(N) can be expressed as

\displaystyle D(N) =(a_1 1) \cdot (a_2  1) \cdot (a_3 1)\dots= \prod_{n=1}^K (a_n 1)

其中an为每个质数的指数。即得以前计算因子数的函数。

private int PrimeFactorisationNoD(int number, int[] primelist) {
    int nod = 1;
    int exponent;
    int remain = number;
 
    for (int i = 0; i < primelist.Length; i++) {
        // In case there is a remainder this is a prime factor as well
        // The exponent of that factor is 1
        if (primelist[i] * primelist[i] > number) {
            return nod * 2;
        }
 
        exponent = 1;
        while (remain % primelist[i] == 0) {
            exponent++;
            remain = remain / primelist[i];
        }
        nod *= exponent;
 
        //If there is no remainder, return the count
        if (remain == 1) {
            return nod;
        }
    }
    return nod;
}

project euler 14 (Which starting number, under one million, produces the longest chain?

n → n/2 (n is even) n → 3n + 1 (n is odd)))

用caching可以减少运行时间,从小到大查找,cache都记录了,每个数到1的步数,所以当数小于自己时,可以用cache查处步数,直接算出。

代码如:

for (int i = 2; i <= number; i++) {
    sequence = i;
    int k = 0;
    while (sequence != 1 && sequence >= i) {//等于1或小于自己时停止(因为小于自己的数的步数都已经算出来)
        k++;
        if ((sequence % 2) == 0) {
            sequence = sequence / 2;
        } else {
            sequence = sequence * 3 + 1;
        }
    }
    //Store result in cache利用cache算出步数(cache[1] = 1)
    cache[i] = k + cache[sequence];
 
    //Check if sequence is the best solution
    if (cache[i] > sequenceLength) {
        sequenceLength = cache[i];
        startingNumber = i;
    }
}

project euler 15(动态规划题)

How many such routes are there through a 20×20 grid?

 Example Grid - Larger subproblem

递加上去,直到顶点


project euler 17(计算letters 1到1000用英文表示)

可以用笔算

 Numbers 1-9

There is no particular pattern in the first numbers, so all we can do is spell them out and count them.

3 + 3 + 5 +4 + 4 +3 +5 +5 +4 = 36

 Numbers 10-19

There is also too little pattern in the next 10 numbers that I want to make a deal out of it, so if we count those as well we get

3 + 6 + 6 + 8 + 8 + 7 + 7 + 9 + 8 + 8 = 70

 Numbers 20-99

Now a pattern starts emerging. For each period of ten numbers, we have a prefix 10 times and then the sequence 1-9, which we have already counted.  So we need to figure out how many letters the tens contain and add 8*36.

10*(6 + 6 + 5 + 5 + 5 + 7 + 6 + 6) + 8*36 = 748

So if we sum that up the number 1-99 will contain 36 + 70 + 74 8= 854 letters

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值