题目链接: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. Ubuntu16.04.1 安装MyCat

    Mycat是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而Mycat并没有存储引擎,所以并不是完全意义的分布式数据库系统. 安装Java环境,配置全局环境变量 MyCAT是使用JAV ...

  2. RUP(Rational Unified Process)统一软件过程概述

    RUP是Rational公司三位杰出的软件工程大师Grady Booch,Ivar Jacobson,James Rumbaugh提出的一个软件工程过程方法.软件开发过程是将一个用户需求转化为软件系统 ...

  3. 节点属性(DOM对象)

    节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...

  4. aspx返回json数据

    JQuery.getJSON 从aspx页面返回JSON数据 . -- ::| 分类: asp.net |举报|字号 订阅 . 发送请求的WebForm1.aspx <%@ Page Langu ...

  5. sql中的系统表sysobjects以及如何查看sql语句的执行时间

    使用sysobjects可以快速查看数据库中表.视图.存储过程.触发器.约束等的信息. 大牛文章:http://www.cnblogs.com/atree/p/SQL-Server-sysobject ...

  6. c位段

    假如程序表示四盏灯的开关状态灯只有开或关两种状态所以用1和0就可以表示为了节省内存就用一个二进制位表示一盏灯这里就定义位域用 a b c d 各表示一盏 这里定义时注意选用无符号类型位域允许用各种格式 ...

  7. Uploadify 3.2 上传图片

    uploadify version: uploadify 3.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  8. mysql语句大全

    转自:http://www.cnblogs.com/yunf/archive/2011/04/12/2013448.html   1.说明:创建数据库 CREATE DATABASE database ...

  9. WPF中使用MVVM模式进行简单的数据绑定

    计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...

  10. android 开发,多个线程共用一个handler

    在做项目过程中,突然发现,项目中启动了多个线程,但是只有一个handler,而不需要每一个线程单独开一个handler,记下笔记: handler = new Handler() { @Overrid ...