欧拉工程第73题:Counting fractions in a range
题目链接:https://projecteuler.net/problem=73
n/d的真分数 ,当d《=12000时 在 1/3 and 1/2 之间的有多少个
public class P73{
void run(){
FareySequences();
}
void FareySequences(){
int limit = 12000;
int a = 1;
int b = 3;
int c = 4000;
int d = 11999;
int count=0;
while(!(c==1 && d==2)){
count ++;
int k = (limit+b)/d;
int e = k*c -a;
int f = k*d -b;
a = c;
b = d;
c = e;
d = f;
}
System.out.println(count);
}
// 7295372
// 117ms
void BruteForce2(){
int max_n = 12000+1;
int result=0;
for(int i=5;i<max_n;i++){
for(int j=i/3+1;j<(i-1)/2+1;j++){
if(gcd(i,j)==1){
result++;
}
}
}
System.out.println(result);
}
// 7295372
// 1923ms
void BruteForce(){
int max_n = 12000+1;
double target2=0.5;
double target1=1/3.0;
int result=0;
for(int i=5;i<max_n;i++){
for(int j=i+1;j<max_n;j++){
if(gcd(i,j)==1){
double tmp = i/(j*1.0);
if(tmp>target1 && tmp<target2)
result++;
}
}
}
System.out.println(result);
}
// 7295372
// 8877ms
int gcd(int a,int b){
int temp;
if(a<b){
temp =a;
a = b ;
b = temp;
}
while(b!=0){
temp = a%b;
a = b;
b = temp;
}
return a;
}
void calculate(){
int max_n = 1000000;
long a = 3;
long b = 7;
long r = 0;
long s = 1;
int q = 0;
long p = 0;
for( q = max_n;q>2;q--){
p = (a*q-1)/b;
if(p*s>r*q){
s = q;
r = p;
}
}
System.out.println(r+"/"+s);
}
boolean isPrime(int num){
if(num==2||num==3) return true;
if(num<2) return false;
for(int i=5;i<Math.sqrt(num)+1;i++)
if(num%i==0) return false;
return true;
}
long[] cal_phi(int max_n){
long[] phi = new long[max_n+1];
for(int i=1;i<max_n;i++){
phi[i] += i;
for(int j =2*i;j<max_n;j+=i)
phi[j]-=phi[i];
}
return phi;
}
public static void main(String[] args){
long t0 = System.currentTimeMillis();
new P73().run();
long t1= System.currentTimeMillis();
System.out.println((t1-t0)+"ms");
}
}
欧拉工程第73题:Counting fractions in a range的更多相关文章
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第72题:Counting fractions
题目链接:https://projecteuler.net/problem=72 真分数;n/d 当d ≤ 1,000,000时候的真分数有多少个 public class P72{ void run ...
- 欧拉工程第71题:Ordered fractions
题目链接:https://projecteuler.net/problem=71 If n<d and HCF(n,d)=1, it is called a reduced proper fra ...
- 欧拉工程第51题:Prime digit replacements
题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
随机推荐
- 【Qt】Qt Linguist介绍【转】
简介 Qt提供了一款优秀的支持Qt C++和Qt Quick应用程序的翻译工具.发布者.翻译者和开发者可以使用这款工具来完成他们的任务. 发布者:承担了全面发布应用程序的责任.通常,他们协调开发者和翻 ...
- PHP实现链式操作的原理
在一个类中有多个方法,当你实例化这个类,并调用方法时只能一个一个调用,类似: db.php <?php class db{ public function where() { //code he ...
- 使用jQuery获取GridView的数据行的数量
一个同事在群里抛出了上述的问题,另一个同事给出了答案,试了一下,还不错.贴出代码和效果图: <html xmlns="http://www.w3.org/1999/xhtml" ...
- linux文件的通用操作方法学习
2014-07-29 23:36:10 在linux下用文件描述符来表示设备文件和普通文件.文件描述符是一个整型的数据,所有对文件的操作都通过文件描述符实现. 文件描述符示文件系统中连接用户空间和内核 ...
- 3. opencv进行SIFT特征提取
opencv中sift特征提取的步骤 使用SiftFeatureDetector的detect方法检测特征存入一个向量里,并使用drawKeypoints在图中标识出来 SiftDescriptorE ...
- Linux使用标准IO的调用函数,分3种形式实现
/*根据cp命令的格式要求,设计一个类cp的功能程序,要求使用标准IO的调用函数,分3种形式实现,字符,行,块.并注意函数功能的分层*/ #include<stdio.h> #includ ...
- margin负值在页面布局中的应用
http://www.w3school.com.cn/tiy/t.asp 预览工具 一.左右列固定,中间列自适应布局 此例适用于左右栏宽度固定,中间栏宽度自适应的布局.由于网页的主体部分一般在中间,很 ...
- PHP获取mysql数据表的字段名称和详细信息的方法
首先我们需要了解下查询MySQL数据库/表相关信息的SQL语句: 代码如下: SHOW DATABASES //列出 MySQL Serv ...
- net windows Kafka
net windows Kafka 安装与使用入门(入门笔记) 完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS 在环境搭建 ...
- ASP.NET Web - 回送
如果希望把更改事件立即传送给服务器,可以把AutoPostback属性设置为true.这样就会使用客户端的JavaScript把窗体数据立即提交给服务器.当然,网络通信量也会增加.使用这个功能时要小心 ...