楼主: tree_new_bee

Euler Project 挨个做- 之二 (Q51-Q78)

[复制链接]
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
51#
发表于 2011-1-1 12:54 | 只看该作者
WOW! 有点绚烂烟花的特效!NB哥拜年都这么NB!

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
52#
 楼主| 发表于 2011-1-1 15:45 | 只看该作者
Q57 Investigate the expansion of the continued fraction for the square root of two.

It is possible to show that the square root of two can be expressed as an infinite continued fraction.

√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...

By expanding this for the first four iterations, we get:

1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...

The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
53#
 楼主| 发表于 2011-1-1 15:51 | 只看该作者
本以为这个很容易, 没想到就这么往上加,加起来很快,不久就溢出了。
之前的大树package中一直没有大数和, 现在加上。(其实实现起来比乘法容易多了)

  function bigplus(d1 varchar2, d2 varchar2) return varchar2 is
    Result varchar2(30000) := '';
    type t_numlist is table of number index by PLS_INTEGER;
    numlist t_numlist;
    i       number;
    lastm   number;
    tmp     number;
  begin
    if d1<1e36 and d2<1e36 then
       return d1+d2;
    end if;
  
    for i in 1 .. greatest(length(d1) ,length(d2)) loop
      numlist(i) := nvl(substr(d1, -i, 1),0) + nvl(substr(d2, -i, 1),0);
    end loop;
  
    lastm := 0;
    for i in 1 .. greatest(length(d1) ,length(d2)) loop
      tmp := mod(numlist(i) + lastm, 10);
      lastm := trunc((numlist(i) + lastm) / 10);
      numlist(i) := tmp;
      result := numlist(i) || result;
    end loop;
    if lastm>0 then result:=lastm||result; end if;
    return(nvl(ltrim(Result, '0'),0));
  end;


观察一下题目中数字的规律, 不难发现, 每对数的分母都是上对数的分子分母和, 而分子则是这个和再加上上一个分母。
然后, 简单的用个递归就行了。

with t(r, a, b)
as (select 1, '3', '2' from dual
union all
select
r+1,
pkg_bignum.bigplus(pkg_bignum.bigplus(a,b),b),
pkg_bignum.bigplus(a,b)
from t where r<1000)
select count(*) from t where length(a)>length(b)
/

  COUNT(*)
----------
       153

Executed in 1.781 seconds

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
54#
 楼主| 发表于 2011-1-1 15:52 | 只看该作者
Q58 Investigate the number of primes that lie on the diagonals of the spiral grid.

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18  5  4  3 12 29
40 19  6  1  2 11 28
41 20  7  8  9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

[ 本帖最后由 tree_new_bee 于 2011-1-1 22:05 编辑 ]

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
55#
 楼主| 发表于 2011-1-1 15:56 | 只看该作者
Q58与Q28的螺旋是一样的, 直接使用Q28的成果就行。

with t(n, sm) as (
select 1, 0 from dual
union all
select
n+1,
sm + case when pkg_prim2.isprim((2*n-1)*(2*n-1)- 2*(n-1))=0 then 1 else 0 end
+ case when pkg_prim2.isprim((2*n-1)*(2*n-1)- 4*(n-1))=0 then 1 else 0 end
+ case when pkg_prim2.isprim((2*n-1)*(2*n-1)- 6*(n-1))=0 then 1 else 0 end  
from t where n<3 or  sm/(4*n-3)>0.1)
select (2*max(n)-1) from t
/

(2*MAX(N)-1)
------------
       26241

Executed in 28.797 seconds

有点慢, 但想不出有什么好的优化手段。

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
56#
发表于 2011-1-1 20:45 | 只看该作者
不知道调用函数的代价如何,其实3、5的倍数是有规律的
with t as
(select level n from dual connect by level <=20)
select ((2*n-1)*(2*n-1)- 6*(n-1)) a,((2*n-1)*(2*n-1)- 4*(n-1)) b,((2*n-1)*(2*n-1)- 2*(n-1))c
from t;

         A          B          C
---------- ---------- ----------
         1          1          1
         3          5          7
        13         17         *21
        31         37         43
        *57         #65         73
        91        101        *111
       133        #145        157
       *183        197        211
       241        257        *273
       307        #325        343
       *381        401        421
       463        #485        *507
       553        577        601
       *651        677        703
       757        #785        *813
       871        901        931
       *993       #1025       1057
      1123       1157       *1191
      1261       1297       1333
      *1407       #1445       1483

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
57#
 楼主| 发表于 2011-1-1 21:51 | 只看该作者
原帖由 〇〇 于 2011-1-1 20:45 发表
不知道调用函数的代价如何,其实3、5的倍数是有规律的
with t as
(select level n from dual connect by level  


isprim的性能,在判断一个非质数时是比较快的, 尤其是那种有2/3/5/7这样的小因子的合数时。
真正费时的是那些真的质数。
所以排除一些3/5的倍数,总体收益并不太多。

这段代码只调用函数40000次左右, 调用次数并不多,函数调用的开销几乎可以忽略。

以下试着预排除3/5/7, 可以看到几乎没有什么作用。

with t(n, sm) as (
select 1, 3 from dual
union all
select
n+1,
sm
+ case when mod((2*n-1)*(2*n-1)- 2*(n-1),3) = 0 or mod((2*n-1)*(2*n-1)- 2*(n-1),5) = 0 or mod((2*n-1)*(2*n-1)- 2*(n-1),7) = 0then 0
when pkg_prim2.isprim((2*n-1)*(2*n-1)- 2*(n-1))=0 then 1 else 0 end
+ case when mod((2*n-1)*(2*n-1)- 4*(n-1),3) = 0 or mod((2*n-1)*(2*n-1)- 4*(n-1),5) = 0 or mod((2*n-1)*(2*n-1)- 4*(n-1),7) = 0 then 0
when pkg_prim2.isprim((2*n-1)*(2*n-1)- 4*(n-1))=0 then 1 else 0 end
+ case when mod((2*n-1)*(2*n-1)- 6*(n-1),3) = 0 or mod((2*n-1)*(2*n-1)- 6*(n-1),5) = 0 or mod((2*n-1)*(2*n-1)- 6*(n-1),7) = 0 then 0
when pkg_prim2.isprim((2*n-1)*(2*n-1)- 6*(n-1))=0 then 1 else 0 end  
from t where n<3 or  sm/(4*n-3)>0.1)
select (2*max(n)-1) from t

(2*MAX(N)-1)
------------
       26241

Executed in 29.328 seconds

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
58#
 楼主| 发表于 2011-1-1 22:13 | 只看该作者
Q59 Using a brute force attack, can you decrypt the cipher using XOR encryption?

不抄原题了, 简单介绍一下:

给出一段密文的ascii码, 已知原文是一段普通英文文本。  密钥是3位小写字母,加密方法是循环使用密钥,用XOR方式加密。

求原文的ascii码的和。

密文文件如下:

cipher1.txt

3.13 KB, 下载次数: 14

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
59#
发表于 2011-1-1 22:19 | 只看该作者
tnb请把工具包放在1楼啊,便于学习..

使用道具 举报

回复
论坛徽章:
10
CTO参与奖
日期:2009-02-20 09:44:20ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30迷宫蛋
日期:2012-05-07 10:55:58茶鸡蛋
日期:2012-04-19 16:08:262012新春纪念徽章
日期:2012-01-04 11:54:462011新春纪念徽章
日期:2011-01-04 10:24:02数据库板块每日发贴之星
日期:2010-12-19 01:01:02数据库板块每日发贴之星
日期:2010-12-13 01:01:012009日食纪念
日期:2009-07-22 09:30:00优秀写手
日期:2014-02-08 06:00:12
60#
 楼主| 发表于 2011-1-1 23:32 | 只看该作者
原帖由 〇〇 于 2011-1-1 22:19 发表
tnb请把工具包放在1楼啊,便于学习..


是说大数package和质数package吧, 我回头整理一下放到顶楼。

使用道具 举报

回复

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

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表