Ocaml stack overflow with easy computations - recursion

Here is my code:
let rec sum n =
if n <= 0 then 0
else if n / 2 * 2 = n then 3 * n + 50 * (sum n-2)
else n + 10 * (sum n-1);;
The math problem is simply as following:
sn =
0 if n = 0
50*sn-2 + 3*n, if n > 0 and n is even
10*sn-1 + n , if n > 0 and n is odd
When I test sum 5, it popped out "stack overflow" error as following:
Stack overflow during evaluation (looping recursion?).
Could anyone help me out?

That is because n is not being changed in the recursive call. You'll have to wrap the n-1 and n-2 in parenthesis. You're calling (sum n)-1 instead of sum (n-1).

Add parentheses:
let rec sum n =
if n <= 0 then 0
else if n / 2 * 2 = n then 3 * n + 50 * (sum (n-2))
else n + 10 * (sum (n-1));;
(* prints 3125 *)
print_int (sum 5);;
Instead of calling sum on n-2 (or n-1), you're calling it on n and subtracting 2 (or 1) from the result. Since the input never changes, it recurses until it overflows the stack.

Related

Goldbach graph using sagemath

I'm learning SageMath (uses Python 3)
and playing with the Goldbach conjecture.
I wrote this function (it works!):
def Goldbach(n):
if n % 2 != 0 or n <= 2:
show("No és parell")
else:
for i in srange(n):
if is_prime(i):
for j in srange(n):
if is_prime(j) and i + j == n:
a = [i, j]
show(a)
return
Now I'm trying (no idea) to do the following plot:
Denoting by r(2k) the number of Goldbach partitions of 2k,
the conjecture affirms that r(2k) > 0 if k > 1.
I have to do a graph of points (k, r(2k)), k > 2.
How could I do it?
First of all, let us get some better implementation in Sage
of the routine counting the number r(K) (for K > 2 some even integer) of the solutions for p + q = 2k, p, q prime numbers.
We count both solutions (p, q) and (q, p) when they differ.
def r(K):
if K not in ZZ or K <= 2 or K % 2:
return None
if K == 4:
return 1
count = 0
for p in primes(3, K):
for q in primes(3, K + 1 - p):
if p + q == K:
count += 1
return count
goldbach_points = [(K, r(K)) for K in range(4, 100,2)]
show(points(goldbach_points))
This gives:

How to convert a bitstream to a base20 number?

Given is a bitstream (continuous string of bits too long to be processed at once) and the result should be a matching stream of base20 numbers.
The process is simple for a small number of bits:
Assuming most significant bit right:
110010011 = decimal 403 (1 * 1 + 1 * 2 + 1 * 16 + 1 * 128 + 1 * 256)
403 / 20 = 20 R 3
20 / 20 = 1 R 0
1 / 20 = 0 R 1
Result is [3, 0, 1] = 3 * 1 + 0 * 20 + 1 * 400
But what if the bits are too much to be converted to a decimal number in one step?
My approach was doing both processes in a loop: Convert the bits to decimal and converting the decimal down to base20 numbers. This process requires the multipliers (position values) to be lowered while walking through the bits, because otherwise, they'll quickly increase too much to be calculated probably. The 64th bit would have been multiplied by 2^64 and so on.
note: I understood the question that a bitstream is arriving of unknown length and during an unknown duration and a live conversion from base 2 to base 20 should be made.
I do not believe this can be done in a single go. The problem is that base 20 and base 2 have no common ground and the rules of modular arithmetic do not allow to solve the problem cleanly.
(a+b) mod n = ( (a mod n) + (b mod n) ) mod n
(a*b) mod n = ( (a mod n) * (b mod n) ) mod n
(a^m) mod n = ( (a mod n)^m ) mod n
Now if you have a number A written in base p and q (p < q) as
A = Sum[a[i] p^i, i=0->n] = Sum[b[i] q^i, i=0->n]
Then we know that b[0] = A mod q. However, we do not know A and hence, the above tells us that
b[0] = A mod q = Sum[a[i] p^i, i=0->n] mod q
= Sum[ (a[i] p^i) mod q, i=0->n] mod q
= Sum[ ( (a[i] mod q) (p^i mod q) ) mod q, i=0->n] mod q
This implies that:
If you want to know the lowest digit b0 of a number in base q, you need to have the knowledge of the full number.
This can only be simplified if q = pm as
b[0] = A mod q = Sum[a[i] p^i, i=0->n] mod q
= Sum[ (a[i] p^i) mod q, i=0->n] mod q
= Sum[ a[i] p^i, i=0->m-1]
So in short, since q = 20 and p = 2. I have to say, no, it can not be done in a single pass. Furthermore, remind yourself that I only spoke about the first digit in base q and not yet the ith digit.
As an example, imagine a bit stream of 1000 times 0 followed by a single 1. This resembles the number 21000. The first digit is easy, but to get any other digit ... you are essentially in a rather tough spot.

How to count the digits of a number recursively in SML

I need to count the digits of a number in a recursive way using Standard ML, assuming that the representation of the number 0 has 1 digit.
fun digitCount 0 = 1
| digitCount n = 1 + digitCount (n div 10)
It would be easy to do if countDigit(0) was 0, but in the code that I wrote the result will always be added by one.
What about using if then else statement instead of pattern-matching on zero value?
fun digitCount n =
if n < 10
then 1
else 1 + digitCount (n div 10)
It's not much more verbose than pattern-matching a can be even written as one-line:
fun digitCount n = if n < 10 then 1 else 1 + digitCount (n div 10)

QBasic - How to find this value?

If we have M as follows:
M = 1+2+3+5+6+7+9+10+11+13+...+n
What would be the QBasic program to find M.
I have done the following so far, but is not returning me the expected value
INPUT "ENTER A VALUE FOR N"
SUM = 0
FOR I = 1 TO N
IF I MOD 4 = 0
SUM = SUM + I
NECT I
How should I go about this?
Thanks.
You have mixed the equality operator. Try this:
INPUT "ENTER A VALUE FOR N"
SUM = 0
FOR I = 1 TO N
IF I MOD 4 <> 0
SUM = SUM + I
NEXT I
No need to write a program, or at least no need to use loops.
Sum of first n natural numbers:
sum_1 = n * (n + 1) / 2
Sum of multiples of 4 < n:
sum_2 = 4 * (n / 4) * (n / 4 + 1) / 2 = 2 * (n / 4) * (n / 4 + 1)
The result is sum_1 - sum_2:
sum = sum_1 - sum_2 = n * (n + 1) / 2 - 2 * (n / 4) * (n / 4 + 1)
NB: / = integer division
This snip calculates the sum of integers to n skipping values divisible by 4.
PRINT "Enter upper value";
INPUT n
' calculate sum of all values
FOR l = 1 TO n
x = x + l
NEXT
' remove values divisible by 4
FOR l = 0 TO n STEP 4
x = x - l
NEXT
PRINT "Solution is:"; x

Calculating Total Number of Times of Loops

I'm trying to calculate the total number of times the innermost statement is executed.
count = 0;
for i = 1 to n
for j = 1 to n - i
count = count + 1
I figured that the most the loop can execute is O(n*n-i) = O(n^2). I wanted to prove this by using double summation but I'm getting lost since the I'm having trouble starting the equation since j = 1 is thrown into there.
Can someone help me explain this to me?
Thanks
For each i, the inner loop executes n - i times (n is constant). Therefore (since i ranges from 1 to n), to determine the total number of times the innermost statement is executed, we must evaluate the sum
(n - 1) + (n - 2) + (n - 3) + ... + (n - n)
By rearranging the terms (grouping all the ns that appear first), we can see that this is equal to
n*n - (1 + 2 + 3 + ... + n) = n*n - n(n+1)/2 = n*(n-1)/2 = n*n/2 - n/2
Here's a simple implementation in Python to verify this:
def f(n):
count = 0;
for i in range(1, n + 1):
for _ in range(1, n - i + 1):
count = count + 1
return count
for n in range(1,11):
print n, '\t', f(n), '\t', n*n/2 - n/2
Output:
1 0 0
2 1 1
3 3 3
4 6 6
5 10 10
6 15 15
7 21 21
8 28 28
9 36 36
10 45 45
The first column is n, the second is the number of times that inner statement is executed, and the third is n*n/2 - n/2.

Resources