Euler 34

答案:40730

 我用程序算了无数次都是145,蛋疼,最后拿别人的程序仔细对比……
原来 !=……
真蛋疼,我竟然连基础数学都忘了

Euler-44

根据公式容易得出:Pmin + Pj = Pk; Pj + Pk = Pmax。

遍历 Pmin 和 Pj,如果 Pmin + Pj 结果是 Pentagonal,那么 Pk = Pmin + Pj;继续算 Pk + Pj 的结果是否等于 Pentagonal 即可。

刚开始以 Pmin 为基准往后求解,算了十多分钟也没算出个结果,吃个饭静了下思路改成从 Pmax 为基准往前求解,秒出结果。

 #include <stdio.h>
#include <math.h> #define MAX_LEN 10000 int isPentagonal(int num)
{
int n = sqrt(num * 2.0 / + 1.0/);
if (*n*n - n == num * )
return n;
n++;
if (*n*n - n == num * )
return n;
return ;
} int main()
{
__int64 n, i, j, k;
__int64 Pentagonal[MAX_LEN]; for (i = ; i < MAX_LEN; i++)
{
Pentagonal[i] = (*i*i-i)/;
for (j = i-; j > ; j--)
{
n = isPentagonal(Pentagonal[i] - Pentagonal[j]);
if (n)
{
n = isPentagonal(Pentagonal[j] - Pentagonal[n]);
if (n)
{
printf("result=%d\n", Pentagonal[n]);
break;
}
}
}
if (j) break;
}
return ;
}

Euler-44

Euler-45

刚开始,直接算,算了一分钟还没出结果。。。

优化了下,秒出结果

 int main()
{
DWORD next_tri = ;
DWORD ind_tri = ;
DWORD next_pen = ;
DWORD ind_pen = ;
DWORD next_hex = ;
DWORD ind_hex = ;
while()
{
next_tri += ++ind_tri;
if (next_tri > next_pen) next_pen += (ind_pen++*)+;
if (next_tri > next_hex) next_hex += (ind_hex++ *)+; if(next_tri == next_pen && next_tri == next_hex)
{
cout<<"solution: "<<next_tri<<endl;
return ;
}
}

Euler-45

 
Euler-46
odd composite 其实是奇数&合数的意思
 #include <stdio.h>
#include <math.h> #define MAX_LEN 10000 int prime[MAX_LEN]; //0-素数, 1-非素数
int goldbach[MAX_LEN]; int main()
{
int i, j, num;
for (i = ; i < MAX_LEN; i++)
{
if (prime[i] == )
{
for (j = i + i; j< MAX_LEN; j+= i)
{
prime[j] = ;
}
}
}
goldbach[] = ;
for (i = ; i < MAX_LEN; i+= )
{
if (goldbach[i] == && prime[i] == ) //不满足i + j*j*2,且为合数
{
printf("result=%d\n", i);
}
if (prime[i] == )
{
for (j = ; j < sqrt(MAX_LEN); j ++)
{
num = i + j*j*;
if (num < MAX_LEN)
{
goldbach[num] = ;
}
}
}
}
return ;
}
 Euler-47
其实就是求每个数有多少个素数公约数
 #include <stdio.h>
#include <math.h> #define MAX_LEN 1000000
#define DISTINCT_COUNT 4 int isPrime[MAX_LEN]; //0-素数, 1-非素数
int num[MAX_LEN]; int main()
{
int i, j, k, count;
for (i = ; i < MAX_LEN; i++)
{
if (isPrime[i] == )
{
for (j = i + i; j < MAX_LEN; j+= i)
{
isPrime[j] = ;
}
for (k = i; k < MAX_LEN; k+= i)
{
num[k] ++;
}
}
} count = ;
for (i = ; i < MAX_LEN; i ++)
{
if (num[i] == DISTINCT_COUNT)
{
count ++;
if (count == DISTINCT_COUNT)
{
printf("result=%d", i - DISTINCT_COUNT + );
break;
}
}
else
{
count = ;
}
} return ;
}

Euler-50

 什么好说的,就是考阅读理解了。
求素数之和,要求连续的素数个数要是最多的,而且连续素数之和也是素数。

Euler-51

暴力解法,400秒,囧。

其实求出两个素数的相同处以后,把不同的位置上分别用0~9去算一遍应该更快,刚想到就出结果了,那也懒的再改了哈哈。

 #include <stdio.h>
#include <math.h> #define MAX_LEN 1000000
#define REPLACEMENTS_LEN 8 int isPrime[MAX_LEN]; //0-素数, 1-非素数 /**获取相同部分,相同部分用数字字符表示,不同部分用'.'表示, 注意结果是倒的*/
char *getdiff(int i, int j)
{
static char diff[] = "";
int count = ;
char diffchar = '\0';
char diffchar2 = '\0';
memset(diff, , sizeof(diff));
while (i > )
{
if (i% == j%)
{
diff[count] = i% + '';
}
else
{
if (diffchar == '\0')
{
diffchar = i % + '';
}
if (diffchar < REPLACEMENTS_LEN-+'' || diffchar != i % + '')
{
return "";
}
if (diffchar2 == '\0')
{
diffchar2 = j % + '';
}
if (diffchar2 < REPLACEMENTS_LEN-+'' || diffchar2 != j % + '')
{
return "";
}
diff[count] = '.';
}
i /= ;
j /= ;
count ++;
}
return diff;
} int check(int num, char *diff, int nth)
{
int idifflen = strlen(diff);
int count = ;
char diffchar = '.';
while (num > )
{
if (diff[count] == '\0')
{
return ;
}
if (diff[count] != '.' && diff[count] != num % + '')
{
return ;
}
if (diff[count] == '.')
{
if (diffchar == '.')
diffchar = num % + '';
if (diffchar != num % + '' || diffchar < REPLACEMENTS_LEN-nth+'')
return ;
}
count ++;
num /= ;
}
if (diff[count] == '\0')
{
return ;
}
return ;
} int main()
{
int i, j, k, count, len, maxlen = ;
char diff[];
for (i = ; i < MAX_LEN; i++)
{
if (isPrime[i] == )
{
for (j = i + i; j < MAX_LEN; j+= i)
{
isPrime[j] = ;
}
}
} /**
*1.找两个素数之间相同的部分
*2.往前找所有素数,如果有相同的部分,记录count
* 注意,如果是两位相同,这两位的数字必然是相同的数字
*/
for (i = ; i < MAX_LEN; i++)
{
if (isPrime[i] == )
{
for (j = i - ; j > ; j -- )
{
count = ;
if (isPrime[j] == )
{
strcpy(diff, getdiff(i, j));
if (diff[] != '\0')
{
count = ;
for (k = j-; k > ; k --)
{
if (isPrime[k] == )
{
count += check(k, diff, count+);
if (count == REPLACEMENTS_LEN) //先用3试试水
{
printf("%d,", k);
break;
}
}
}
}
if (count == REPLACEMENTS_LEN)
{
printf("%d,", j);
break;
}
}
}
}
if (count == REPLACEMENTS_LEN)
{
printf("%d\n", i);
break;
}
} //printf("%d", check(56663, "3..65")); return ;
}

Project Euler的更多相关文章

  1. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  4. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  5. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  6. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  7. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  8. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  9. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  10. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

随机推荐

  1. poj2676 Sudoku(搜索)

    题目链接:http://poj.org/problem?id=2676 题意:9*9的方格,0代表没数字,其他代表数字,请在格子中填入1~9的数字,使得在每行,每列和每个3*3的方块中,1~9的数字每 ...

  2. rpm安装总结(转载)

    转自:http://www.cnblogs.com/nuke/archive/2009/03/03/1402067.html 在RedHat Linux和Mandrake等兼容RedHat的发行版中, ...

  3. Step 4: Install and Configure Databases

    https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_installing_configuring_dbs.htm ...

  4. dllMain函数

    Windows在加载DLL的时候,需要一个入口函数,就如同控制台或DOS程序需要main函数.Win32程序需要WinMain函数一样.一些例子中,DLL并没有提供DllMain函数,应用工程也能成功 ...

  5. 使用Apache Commons IO组件读取大文件

    Apache Commons IO读取文件代码如下: Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new ...

  6. synchronized(1)用法简介:修饰方法,修饰语句块

    注意: 同一个对象或方法在不同线程中才出现同步问题,不同对象在不同线程互相不干扰. synchronized方法有2种用法:修饰方法,修饰语句块 1.synchronized方法 是某个对象实例内,s ...

  7. C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用

    public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...

  8. .Net Framework Client Profile 和 .Net Framework的区别[转]

    原文链接 VS2010默认是以.Net Framework Client Profile为生成环境的,如果需要更多的功能,应该调用.Net Framework.

  9. API系列一:REST和RESTful认识

    序言 最近工作的项目一直使用API,就想趁這个机会,把API的知识点进行一次梳理和总结,顺便提升一下自己对API全新的认识 Web API 是ASP.NET平台新加的一个特性,它可以简单快速地创建We ...

  10. 程序猿工具——svn

    一个项目的产生,都需要团队中的开发人员互相协作.它的简单,方便深深吸引了我. svn的使用,有2部分组成--svn服务器.svn客户端.svn服务器一般团队之间只要有一个安装就可以了. 在学习安装sv ...