b * sigma to floor(n/b)
so that the expression:
4 + 8 + 12 + 16 + ... 100
becomes 4(1 + 2 + 3 + 4 + ... 25)
Note that arithmetic sum can be re-expressed as n(n+1)/2
1
2
3
4
2
3
4
function sigmam(n, b) 	local function sigma(n) return n*(n+1)/2 end 	return b*sigma(math.floor(n/b)) end
New Challenges:
Challenge 6: (Easy-Medium)
Write a function sigmams(n,ms) that returns the sum of all multiples of at least one number in the table ms so that:
sigmams(10,{2,3}) = 2 + 3 + 4 + 6 + 8 + 9 + 10
Hint: Exclude 6 in the above case
Challenge 7: (Easy-Medium)
Assume that we have a recursive function k(n) that generates a set of numbers with the following rule:
k(1) = 1
k(n) = n, k(n/2) if n is even
k(n) = n, k(3n+1) if n is odd
so that the first 5 terms of the k-series is as follows:
k(1) = 1
k(2) = 2 1
k(3) = 3 10 5 16 8 4 2 1
k(4) = 4 2 1
k(5) = 5 16 8 4 2 1
Assume that for every n in the set of positive integers, k(n) returns a finite set that terminates with 1, write a function maxCycle(a,b) that finds the index n between a and b (a<=n<=b) for which k(n) contains the greatest number of elements (the longest cycle)
Challenge #8: (Easy)
Write a function Windows() that returns true if the OS is not a *nix derivative (Linux, Symbian, iOS, Mac, Debian, etc) and false if it is.
edited 1×, last 02.07.10 07:30:25 am