寄托天下
楼主: ygc
打印 上一主题 下一主题

开贴做题,囧~~~ [复制链接]

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

16
发表于 2008-7-31 20:26:51 |只看该作者

再不来贴就沉下去了。。。

Problem 14.  Find the longest sequence using a starting number under one million.
The following iterative sequence is defined for the set of positive integers:

n->n/2 (n is even)
n->3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13->40->20->10->5->16->8->4->2->1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.


这个题,照着问题写代码就可以了,不过算得比较慢,我没能想出什么好的方法来加速它。

=============================================================================================


n <- 1000000
i <- 1
count <- 0
long_chain_num <- 0      
while(i <n){
    old.i <- i
    cnt <- 2      ## i and 1 are not count in the while loop...
    while( i != 1 ){
        if(i %% 2) {
            i <- 3*i +1
        } else{
            i <- i /2

        }
        cnt = cnt + 1
    }
    if (count < cnt){
        count <- cnt
        long_chain_num <- old.i
    }
    i <- old.i + 1
}

cat (long_chain_num,"\n")

Answer: 837799

使用道具 举报

Rank: 10Rank: 10Rank: 10

声望
1768
寄托币
63835
注册时间
2008-4-28
精华
1
帖子
8456

Golden Apple

17
发表于 2008-7-31 20:30:00 |只看该作者
大叔在干什么?:funk:

使用道具 举报

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

18
发表于 2008-7-31 20:38:06 |只看该作者
做数学题。。。。

第一贴上有链接。。。

使用道具 举报

Rank: 10Rank: 10Rank: 10

声望
1768
寄托币
63835
注册时间
2008-4-28
精华
1
帖子
8456

Golden Apple

19
发表于 2008-8-1 21:10:44 |只看该作者
:eek: :funk: 赞,勤奋的大叔

使用道具 举报

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

20
发表于 2008-8-4 18:53:46 |只看该作者
偶不来更新了。。

以后贴blog里。:o

使用道具 举报

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

21
发表于 2008-8-22 21:12:20 |只看该作者
:(

使用道具 举报

Rank: 10Rank: 10Rank: 10

声望
1768
寄托币
63835
注册时间
2008-4-28
精华
1
帖子
8456

Golden Apple

22
发表于 2008-12-23 14:29:15 |只看该作者
大叔终于做完鸟?:confused: :rolleyes: ;d:

使用道具 举报

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

23
发表于 2008-12-29 16:09:57 |只看该作者

回复 #22 Spectoria 的帖子

早不来贴了。。

没做完的说。。。
只做了40多道而已。

使用道具 举报

Rank: 10Rank: 10Rank: 10

声望
1768
寄托币
63835
注册时间
2008-4-28
精华
1
帖子
8456

Golden Apple

24
发表于 2008-12-30 00:22:47 |只看该作者
40多道:eek:
勤奋的大叔Orz

使用道具 举报

Rank: 8Rank: 8

声望
254
寄托币
5215
注册时间
2007-9-8
精华
0
帖子
359

Golden Apple

25
发表于 2008-12-30 10:30:46 |只看该作者
前面的题好简单,越来越难。。后面的超出我的能力了。。。囧。。。

使用道具 举报

Rank: 10Rank: 10Rank: 10

声望
1768
寄托币
63835
注册时间
2008-4-28
精华
1
帖子
8456

Golden Apple

26
发表于 2008-12-30 14:38:18 |只看该作者

回复 #25 ygc 的帖子

;d: patpat :handshake

使用道具 举报

RE: 开贴做题,囧~~~ [修改]

问答
Offer
投票
面经
最新
精华
转发
转发该帖子
开贴做题,囧~~~
https://bbs.gter.net/thread-862749-1-1.html
复制链接
发送
回顶部