计算机数学和算法习题和解答(1~3)
2012-10-22 11:05
246 查看
1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
找到小于1000的 而且 是3或者5的倍数的 自然数,求它们的和。
x <- 1
sum <- 0
while (x < 1000) {
if (x%%3==0 | x%%5==0) {
sum <- sum + x
}
x <- x + 1
}
print(sum)
2. Each new term in the Fibonacci sequence(http://baike.baidu.com/view/1074762.htm) is generated by adding the previous
two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
有一个小于400万的斐波那契数列,求其中的偶数和。
i <- 2
x <- 1:2
while (x[i]<4000000) {
x[i+1] <- x[i-1]+x[i]
i <- i+1
}
sum(x[x%%2==0])
3. The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
求600851475143的最大质数因子。
findprime <- function(x) {
if (x %in% c(2,3,5,7)) return(TRUE)
if (x%%2 == 0 | x==1) return(FALSE)
xsqrt <- round(sqrt(x))
xseq <- seq(from=3,to=xsqrt,by=2)
if (all(x %% xseq !=0)) return(TRUE)
else return(FALSE)
}
# x = 1:111
# x[sapply(x,findprime)]
maxfactor <- 0
n <- 110
for (i in seq(from=3, to=round(sqrt(n))+1, by=2)) {
if (findprime(i) & (n %% i == 0)) {
#i是质数因子
n <- n / i
maxfactor <- i
if (i >= n) break
}
}
print(maxfactor)
参考文章求最大质因子的N种境界
Find the sum of all the multiples of 3 or 5 below 1000.
找到小于1000的 而且 是3或者5的倍数的 自然数,求它们的和。
x <- 1
sum <- 0
while (x < 1000) {
if (x%%3==0 | x%%5==0) {
sum <- sum + x
}
x <- x + 1
}
print(sum)
2. Each new term in the Fibonacci sequence(http://baike.baidu.com/view/1074762.htm) is generated by adding the previous
two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
有一个小于400万的斐波那契数列,求其中的偶数和。
i <- 2
x <- 1:2
while (x[i]<4000000) {
x[i+1] <- x[i-1]+x[i]
i <- i+1
}
sum(x[x%%2==0])
3. The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
求600851475143的最大质数因子。
findprime <- function(x) {
if (x %in% c(2,3,5,7)) return(TRUE)
if (x%%2 == 0 | x==1) return(FALSE)
xsqrt <- round(sqrt(x))
xseq <- seq(from=3,to=xsqrt,by=2)
if (all(x %% xseq !=0)) return(TRUE)
else return(FALSE)
}
# x = 1:111
# x[sapply(x,findprime)]
maxfactor <- 0
n <- 110
for (i in seq(from=3, to=round(sqrt(n))+1, by=2)) {
if (findprime(i) & (n %% i == 0)) {
#i是质数因子
n <- n / i
maxfactor <- i
if (i >= n) break
}
}
print(maxfactor)
参考文章求最大质因子的N种境界
相关文章推荐
- 「算法竞赛入门经典 第二版」第 3 章 数组和字符串 习题解答
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.7
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.15
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.20
- 深入理解计算机系统第二版习题解答CSAPP 2.19
- 《慕课网玩转算法面试》笔记及习题解答8.1~8.3
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.5.7
- [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.23
- 「算法竞赛入门经典 第二版」第 1、2章 循环结构程序设计 习题解答
- 《慕课网玩转算法面试》笔记及习题解答6.4.~6.6
- 算法导论6.2-5习题解答(堆排序)
- 算法导论7.1-4习题解答(快速排序)
- 深入理解计算机系统第二版习题解答CSAPP 2.12
- 计算机算法基础 ——数学(排列组合函数)
- 数据结构和算法分析习题解答之一——引言
- c++primer第四版第三章课后习题的几个解答算法
- 《慕课网玩转算法面试》笔记及习题解答9.3
- 数学之美---计算机图形学算法(转贴)
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.5.8
- [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.24