Octal number equations - octal

So I'm reading a book on how binary bits are converted into octal numbers.
When trying to explain the concept, they give this equation
N= S(...((d8)2^8+(d7)2^7+(d6)2^6)+((d5)2^5+(d4)2^4+(d3)2^3)+((d2)2^2+(d1)2^1+d0))
or
N= S(...((d8)2^2 +(d7)2+(d6))2^6 + ((d5)2^2 +(d4)2^1 + (d3))2^3 + ((d2)2^2+(d1)2^1+d0))
d represents the digit found within the bit, e.g. if the least significant bit was 1, then (d0) would be 1.
I understand all of this, but they elaborate further saying that the parenthesized expressions ((d8)2^2 +(d7)2+(d6)) are coefficients of base 8 digits, N=S((d2)8^2+(d1)*8+(d0)).
Can someone explain what they mean by the parenthesized expressions being coefficients of base8 digits?

The digits di are the binary digits of the number. We can compute the number from its binary digits like this:
n = ∑ i 2i di = 20 d0 + 21 d1 + 22 d2 + ⋯
(This is in fact what defines “binary”, if we add the condition that the digits are integers and 0 ≤ di < 2 for all i.)
Suppose we name the octal digits of the number oj. We can compute the number from its octal digits like this:
n = ∑ j 8j oj = 80 o0 + 81 o1 + 82 o2 + ⋯
(This is what defines “octal”, if we add the condition that the digits are integers and 0 ≤ oj < 8 for all j.)
Now let's look back at the binary equation. The first step is the trickiest. We will change the way the subscript is used so that each term of the summation uses three binary digits:
n = ∑ j 23 j + 0 d3 j + 0 + 23 j + 1 d3 j + 1 + 23 j + 2 d3 j + 2
Convince yourself that that equation computes the same n as the first equation I gave.
I assume you know that xa + b = xa xb. So we can separate those 23 j + b coefficients like this:
n = ∑ j (23 j 20) d3 j + 0 + (23 j 21) d3 j + 1 + (23 j 22) d3 j + 2
Then we can factor out the 23 j term like this:
n = ∑ j 23 j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
I assume you also know that xa b = (xa)b. So we can split the 23 j term like this:
n = ∑ j (23)j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
And we can simplify 23 to 8:
n = ∑ j 8j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
Compare this to the formula for computing the number from its octal digits, which I repeat here:
n = ∑ j 8j oj
So we can conclude this:
oj = 20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2
For example, let's take j = 2:
o2 = 20 d3×2 + 0 + 21 d3×2 + 1 + 22 d3×2 + 2 = 20 d6 + 21 d7 + 22 d8

Related

Minizinc constraint against recursive function

I want to use a function like this:
function int: nextr(var int: n)
if n <= 1
2
elseif n <= 8
n + 5
elseif n <= 68
n + 13
elseif n <= 509
n + 34
elseif n <= 3611
n + 89
else n + 233
in a constraint that variable must satisfy any value in nextr(n), nextr(nextr(n)), nextr(next(nextr(n))), and so on.
Is there a way to specify such constraint in minizinc? If not possible generally, I'm OK with explicit recursion limit, but without tedious enumeration of all the steps?
Example:
The value of y is constrained to be equal
next(x) \/ next(next(x)) \/ ...
up to K levels of nesting.
function var int: nextr(var int: n) =
if n <= 1 then
2
elseif n <= 8 then
n + 5
elseif n <= 68 then
n + 13
elseif n <= 509 then
n + 34
elseif n <= 3611 then
n + 89
else
n + 233
endif;
int: K = 10;
var int: x;
var int: y;
array[1..K] of var int: rec_up_to_k;
constraint forall (i in 1..K) (
if i == 1 then
rec_up_to_k[i] = nextr(x)
else
rec_up_to_k[i] = nextr(rec_up_to_k[i-1])
endif
);
constraint exists (i in 1..K) (
y = rec_up_to_k[i]
);
constraint x >= 0;
solve satisfy;
outputs:
x = 3612;
y = 3845;
rec_up_to_k = array1d(1..10, [3845, 4078, 4311, 4544, 4777, 5010, 5243, 5476, 5709, 5942]);
----------

Project Euler #50, prime sums not correct?

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive
primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to
a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most
consecutive primes?
I'm using Racket (a dialect of scheme) for this example, but this should be language agnostic. In the question, it states that the sum of the first 21 consecutive primes is 953. So, I went to test this out (I had already written my code for this problem and it was working incorrectly).
> (define primes (filter prime? (range 2 10000)))
> (apply + (take primes 6)) ; This is 41: Good so far!
> ; This is where it gets odd.
> (apply + (take primes 21)) ; This is 712. And, after further experimentation, there is amount of summed primes that is 953.
> (apply + (take primes 23)) ; This is 874.
> (apply + (take primes 24)) ; This is 963.
Is there something I'm missing about the question?
Euler #50 asks for sums of consecutive primes, which need not necessarily begin with the first prime. The fact that the shown example starts with the first prime is incidental (although it is no accident that the winning sequence starts with at a small prime).
953 = 7 + 11 + 13 + 17 + 19 + 23 + 29
+ 31 + 37 + 41 + 43 + 47 + 53 + 59
+ 61 + 67 + 71 + 73 + 79 + 83 + 89
That's 21 terms. There is no mistake in the problem description - the term 'first' does not appear anywhere in the text.
You read the question wrong. The sum must also be a prime, which 963 is not (107 * 9, for example).

Numerical solutions of a nonlinear equation with different independent values in matlab

For example, if I have this function: g = t^3 - 5*t^2 + 2
And g = [3 4 6 2 9 10 17 1]
I would like to solve the equation for each g[i] and obtain the resulting t vector.
This might guide you:
>> syms t g %// define symbolic variables
>> y = t^3 - 5*t^2 + 2 - g; %// define y so that equation is: y=0
>> g_data = [3 4 6 2 9 10 17 1]; %// define g values
>> n = 1; %// choose first value. Or use a loop: for n = 1:numel(g_data)
>> s = solve(subs(y, g, g_data(n))) %// substitute g value and solve equation y=0
s =
25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3) + 5/3
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
>> double(s) %// show solutions as floating point values
ans =
5.039377328113847
-0.019688664056924 + 0.445027607060817i
-0.019688664056924 - 0.445027607060817i

Have a trouble with the function roots

Hey guys I have multiple problems with using function 'roots'.
I Have to find zeros of 's^1000 + 1'.
I made Y = zeros(1,1000) then manually changed the 1000th matrice to '1'. but then 'root' function does not work with it !
Another problem is that I am having trouble with matrix multiplication. The question is finding zeros(roots) of (s^6 + 6*s^5 + 15*s^4 + 20*s^3 + 15*s^2 + 6*s +1)*(s^6 + 6s^5 + 15*s^4 +15*s^2 +6*s +1) so i did:
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
roots(y)
but this gives me
-27.9355 + 0.0000i
-8.2158 + 0.0000i
0.1544 + 0.9880i
0.1544 - 0.9880i
-0.1217 + 0.0000i
-0.0358 + 0.0000i
where I calculate the original equation with wolfram then I have made matrix as :
p = [1 12 66 200 375 492 524 492 375 200 66 12 1]
roots(p)
and this gives me :
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051 + 0.0000i
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9975 + 0.0044i
-0.9975 - 0.0044i
-0.9949 + 0.0000i
-0.1943 + 0.1539i
-0.1943 - 0.1539i
and I think the second solution is right (that is what wolfram alpha gave me)
How would you answer these two questions through matlab guys?
To multiply polynomials, you convolve their coefficients:
>> roots(conv(a,b))
ans =
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9974 + 0.0044i
-0.9974 - 0.0044i
-0.9950
-0.1943 + 0.1539i
-0.1943 - 0.1539i
Q1
Using roots to find the roots of s1000 + 1 is a bit of an overkill. The solution is given by this code snippet (corrected the first version; may be deduced using De Moivre's formula):
n = 1000;
k = 0:n-1
u = (2*k + 1)*pi / n;
s = cos(u) + 1i*sin(u)
Also, this method is approx. 100000 times faster.
Q2
Multiplying two polynomials to find the roots of their product is a bit of an overkill. :-) The union of the two polynomials' root sets is the root set of the product polynomial:
s = [roots(a);roots(b)]
Also, this method is more accurate.
1) The vector describing s^1000 + 1 should end with a 1 as well.
2)
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
This is a DOT product, multiplication of polynomials do not multiply element-wise.
Question 1
You need to include the coefficient of x^0 in the vector of coefficients, so there are 1001 entries with the first and last being 1
coeffs=zeros(1001,1);
coeffs([1,1001])=1;
roots(coeffs)
Question 2
To multiply the coefficients of polynomials you need to use convolution:
roots(conv(a,b))

Problem with numbers

I am given a number N, and i must add some numbers from the array V so that they wil be equal. V is consisting of numbers that are all powers of 3:
N = 17
S = 0
V = 1 3 9 27 81 ..
I should add numbers from V to N and S in order to make them equal. The solution to the example above is :
17 + 1 + 9 = 27, 27, 1 and 9 are taken from V, a number from V can be taken only once, and when taken it's removed from V.
I tried sorting V and then adding the biggest numbers from V to S until S has reached N, but it fails on some tests when it's like:
N = 7
S = 0
V = 1 3 9 27
So the solution will be:
7 + 3 = 9 + 1
In examples like this i need to add numbers both to N and S, and also select them so they become equal.
Any idea of solving this ? Thanks.
Write N in base 3: 17 = 2*1 + 2*3 + 1*9
Find the first power of 3 with coefficient 2, in this case 1.
Add this power of 3: 17 + 1
Repeat until all coefficients are 0 or 1.
17 = 2*1 + 2*3 + 1*9
17 + 1 = 2*9
17 + 1 + 9 = 27
7 = 1*1 + 2*3
7 + 3 = 1*1 + 1*9