欧拉工程第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 ...
随机推荐
- LitJSON使用
地址:http://lbv.github.io/litjson/docs/quickstart.html LitJSON Quickstart Guide Introduction Quick Sta ...
- ERP基本功——物料的四个量
ERP基本功——物料的四个量 在分析制造业管理问题的时候,如果能借用一些ERP里才会用到的概念,就会非常的简单,并且条理清晰. 我觉得柳中冈先生一再强调的一个观点非常有用,大意是这样的:任何人依据现有 ...
- [DevExpress]ChartControl之柱状图示例
关键代码: using System; using System.Data; using System.Windows.Forms; using CSharpUtilHelpV2; using Dev ...
- Oracle 动态视图5 V$SESSION_LONGOPS
一.视图V$SESSION_LONGOPS显示运行超过6秒的操作的状态.包括备份,恢复,统计信息收集,查询等等 Column Datatype Description SID NUMBER Sessi ...
- 图解 CSS: 理解样式表的逻辑(转载)
原文:http://www.cnblogs.com/del/archive/2009/02/01/1382141.html 样式表可以是外部的.内联的或嵌入的; 链接外部样式文件一般是:<lin ...
- 将开始我的WebForm控件开发之旅
时间总是过得很快,一转眼三个月就过去了,三个月内发生了很多的事.因为学校的学习,离开了我入门WPF的公司:开发了第一个外包项目,做的是WebForm的:而且了马上要毕业了,毕业后的公司应该是专门用We ...
- 《.NET简单企业应用》技术路线
前言 工作三年了,一直从事基于.NET体系的企业应用开发,心得和经验也攒了点:担心时间长了给忘了,所以得给写下来,以便以后回味回味:更重要的是能让知识系统化和体系化. 本系列以一个简单的企业应用系统为 ...
- 自定义控件(模仿微信ToggleButton控件)
弄过android开发的都知道,系统有一个默认的ToggleButton,但很多人都觉得他很难看,当然也包括我.如果你感觉他不难看,那你就继续使用系统的吧,这篇文章对你来说是多余的了. 今天来写一个模 ...
- WPFMediaKit照相功能
最近写的一个WPF照相功能,往各位吐槽,提供优化 在WPF 设计器中添加如下代码 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.C ...
- mysql Host ‘XXXXXX’ is blocked because of many connection errors
mysql Host ‘XXXXXX’ is blocked because of many connection errors ERROR 1129 (00000): Host ‘XXXXXX’ i ...