题目链接: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的更多相关文章

  1. (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 ...

  2. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  3. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  4. 欧拉工程第72题:Counting fractions

    题目链接:https://projecteuler.net/problem=72 真分数;n/d 当d ≤ 1,000,000时候的真分数有多少个 public class P72{ void run ...

  5. 欧拉工程第71题:Ordered fractions

    题目链接:https://projecteuler.net/problem=71 If n<d and HCF(n,d)=1, it is called a reduced proper fra ...

  6. 欧拉工程第51题:Prime digit replacements

    题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...

  7. 欧拉工程第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 ...

  8. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  9. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

随机推荐

  1. 如何在C#中实现图片缩放

    //下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着 //************************************************************// /// ...

  2. Delphi XE5教程3:实例程序

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  3. kdbchk: the amount of space used is not equal to block size

    一.对数据文件检查 注意:应该在关闭数据库模式下进行bbed的操作 [oracle@ora10 controlfile]$ dbv file=/u01/app/oracle/oradata/ORCL/ ...

  4. Python脚本控制的WebDriver 常用操作 <十三> 处理button group层的定位

    下面将使用webdriver来定位同一层的按钮 测试用例场景 button group就是按钮组,将一组按钮排列在一起. 处理这种对象的思路一般是先找到button group的包裹(wrapper) ...

  5. Ubuntu12.10编译openwrt遇到的错误

    由于Openwrt有很多工具是要先编译的,在Ubuntu12.10平台下编译openwrt时就遇到了下面这样的错误:elf.cpp: In static member function 'static ...

  6. IE下同样的$.ajax()被调用两次,只能执行一次(第一次)

    今天发现了这个问题,仅限于IE下所有浏览器包括Edge 百度了一下原来问题就在这句话:如果第二次请求与第一次请求完全相同,会直接从缓存获取. 那么就在请求时让URL变得不一样吧 $.ajax({ ty ...

  7. 【http】http/1.1 八种请求方式

    OPTIONS 返回服务器针对特定资源所支持的HTTP请求方法.也可以利用向Web服务器发送'*'的请求来测试服务器的功能性. HEAD 向服务器索要与GET请求相一致的响应,只不过响应体将不会被返回 ...

  8. ASP.NET MVC +EasyUI 权限设计(三)基础模块

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...

  9. Javascript中字符串转换成Date的方法

    //字符串转成Time(dateDiff)所需方法 function stringToTime(string) { var f = string.split(' ', 2); var d = (f[0 ...

  10. FFmpeg在Android上的移植之第一步

    http://blog.sina.com.cn/s/blog_69a04cf40100x1fr.html 从事多媒体软件开发的人几乎没有不知道FFmpeg的,很多视频播放器都是基于FFmpeg开发的. ...