鱼C论坛

 找回密码
 立即注册
查看: 4017|回复: 9

题目73:在1/3和1/2之间有多少个最简真分数?

[复制链接]
发表于 2015-11-5 16:49:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Counting fractions in a range

Consider the fraction, n/d, where n and d are positive integers. If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 3 fractions between 1/3 and 1/2.

How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d ≤ 12,000?

题目:

考虑分数 n/d, 其中 n 和 d 是正整数。如果 n<d 并且最大公约数 HCF(n,d)=1, 它被称作一个最简真分数。

如果我们将 d ≤ 8 的最简真分数按照大小的升序列出来,我们得到:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

可以看出 1/3 和 1/2 之间共有 3 个分数。

在 d ≤ 12,000 的升序真分数列表中,1/3 和 1/2 之间有多少个分数?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-25 04:14:17 | 显示全部楼层
  1. 7295372
  2. [Finished in 9.6s]

  3. lst = []
  4. for i in range(3,12001):
  5.         for j in range (int(i/3),int(i/2+1)):
  6.                 if j/i < 0.5 and j/i > 1/3:
  7.                         lst.append(j/i)
  8. lst = set(lst)
  9. print (len(lst))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-16 14:09:36 | 显示全部楼层
这题如果用欧拉函数算会更快,不过暴力算法9.6s也还可以,就不搞复杂了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-6 00:05:33 | 显示全部楼层

题目不是要求j/i是最简真分数吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-6 00:07:12 | 显示全部楼层

你这个结果是对的,但过程有点看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-19 11:13:34 | 显示全部楼层
  1. from math import gcd
  2. print(sum(map(lambda x: len(list(filter(lambda y: gcd(y, x) == 1,
  3.                                         range(x // 3 + 1, (x + 1) // 2)))),
  4.               range(1, 12001))))
复制代码

https://github.com/devinizz/project_euler/blob/master/page02/73.py
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-9 15:56:45 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>

  4. // 断一个数是否是真分数,返回判断标志,同时返回该分数
  5. bool IsProFrac(int a, int b){
  6.     if(b%a == 0 ){
  7.         if(a == 1){
  8.             return true;
  9.         }
  10.         return false;
  11.     }
  12.     return true;
  13. }


  14. void  AllProFrac(int d, float array[100]){
  15.     int n = 0;
  16.     for(int i = 2; i<= d; i++){
  17.         for(int j = 1;j<i;j++){
  18.             if(IsProFrac(j,i)){
  19.                 array[n]=((float)j)/i;
  20.                 printf("[%d,%d]\n",j,i);
  21.                 n++;
  22.             }
  23.         }
  24.     }
  25.     return;
  26. }


  27. void Print(float array[100]){
  28.     for(int i = 0; i<100;i++){
  29.         printf("%5.2f ",array[i]);
  30.         if((i+1)%10 == 0){
  31.             printf("\n");
  32.         }
  33.     }
  34. }

  35. void maini)
  36.     float array[100]={0};
  37.     AllProFrac(12,array);   
  38.     Print(array);
  39.     for(int i = 0; i<100; i++){
  40.         if(array[i]<(float)1/2 && array[i] >(float)1/3){
  41.             printf("%5.4f ",array[i]);

  42.         }
  43.     }
  44.         printf("\n");
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-31 17:42:16 | 显示全部楼层
7295372

Process returned 0 (0x0)   execution time : 11.403 s
Press any key to continue.
比71题多了一些工作,找出上下界表达式,逐一判断互质
  1. #include<iostream>
  2. #include<set>
  3. using namespace std;

  4. const int M = 1e6;
  5. const int RANGE = 12000;
  6. int ct = 0;
  7. bool prime[M];
  8. int factor[M];
  9. set<int> fac_n;

  10. void euler_sieve(){
  11.   prime[1] = false;
  12.   for (int i = 2;i <= M;i++) prime[i] = true;

  13.   for (int i = 2;i <= M;i++){
  14.     if (prime[i]) factor[++ct] = i;
  15.     for (int j = 1;j <= ct && i*factor[j] < M;j++){
  16.       prime[i*factor[j]] = false;
  17.       if (i % factor[j] == 0) break;
  18.     }
  19.   }
  20. }

  21. bool co_prime(int x,int y){//x < y
  22.   if (prime[y]) return true;
  23.   if (prime[x] && y % x)  return true;
  24.   if (y == x*2 + 1) return true;

  25.   for (set<int>::iterator it = fac_n.begin();it != fac_n.end();++it){
  26.     if (y % *it == 0) return false;
  27.   }
  28.   return true;
  29. }

  30. void parse(int x,set<int> & s){//分解质因数
  31.   for (int i = 1; ;i++){
  32.     int t = factor[i];
  33.     while(x % t == 0) {s.insert(t); x /= t;}
  34.     if (x == 1 || prime[x]) break;
  35.   }
  36.   if (prime[x]) s.insert(x);
  37. }

  38. int main(){
  39.   euler_sieve();
  40.   int ans = 0;

  41.   for (int d = 5;d <= RANGE;d++){
  42.     int lb = d/3 + 1,ub = (d-1) / 2;
  43.     //cout << d << " " << lb << " " << ub << endl;
  44.     for (int n = lb;n <= ub;n++){
  45.       fac_n.clear();
  46.       parse(n,fac_n);
  47.       if (co_prime(n,d))  ans++;
  48.     }
  49.   }
  50.   cout << ans << endl;
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-31 17:26:00 From FishC Mobile | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-12-31 18:03 编辑
  1. #include<iostream>



  2. bool judge(int a, int b) {
  3.     int c;

  4.     while (b) {
  5.         c = a % b;
  6.         a = b;
  7.         b = c;
  8.     }

  9.     return a == 1;
  10. }



  11. int main {
  12.     using namespace std;

  13.     int deno, nume, max, count = 0;


  14.     for (deno = 4; deno <= 12000; deno++) {
  15.         max = deno >> 1;

  16.         if (deno & 1) {
  17.             max++;
  18.         }

  19.         for (nume = deno / 3 + 1; nume <= max; nume++) {
  20.             count += judge(nume, deno);
  21.         }
  22.     }


  23.     cout << count << endl;
  24.     return 0;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 12:15:05 | 显示全部楼层
本帖最后由 guosl 于 2021-3-21 12:45 编辑
  1. /*
  2. 答案:7295372
  3. 耗时:0.812547秒(单核)
  4.       0.135768(8核)
  5. */
  6. #include <iostream>
  7. #include<algorithm>
  8. #include <omp.h>
  9. using namespace std;

  10. int gcd(int a, int b)
  11. {
  12.   if (b == 0)
  13.     return a;
  14.   int c = a % b;
  15.   while (c != 0)
  16.   {
  17.     a = b;
  18.     b = c;
  19.     c = a%b;
  20.   }
  21.   return b;
  22. }

  23. int main(void)
  24. {
  25.   double t = omp_get_wtime();
  26.   int nCount = 0;
  27. #pragma omp parallel for reduction(+:nCount) schedule(guided,4)
  28.   for (int i = 1; i < 12000; ++i)
  29.   {
  30.     for (int j = 2 * i + 1; j < min(3 * i, 12001); ++j)
  31.     {
  32.       if (gcd(i, j) == 1)
  33.         ++nCount;
  34.     }
  35.   }
  36.   t = omp_get_wtime() - t;
  37.   cout << nCount << endl << t << endl;
  38.   return 0;
  39. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表