Subtract / Divide two lists of digits - recursion

I want to represent numbers as lists of digits. How can I add subtract and divide the two numbers even if they are of different size ?
For example : 100 - 12 = 88 is equivalent to (1 0 0 ) minus ( 1 2) = ( 8 8)
100 / 12 = 8 is equivalent to (1 0 0 ) divided by (1 2 ) = (8)

There are two ways to represent numbers as digits in decimal. Lets imagine I want to represent the number 123. One obvious one is to do it from most significant digit to least like (1 2 3). Thats good for presentation only. The other way is to do it least to most significant (3 2 1).
When adding and substracting you add from the least significant digits and if the sum is above 9 you carry (add 1 to the next digit in one of the numbers) and continue recursing.
So do you know how to long add? If so this will be a piece of cake to do. 123 + 49 is (+ '(9 4) '(3 2 1)) ; ==> (2 7 1). Good luck!

Related

How to find actual value of integers participated in sum operation

I have integers with power 2^n where n = 0 to 5
Lets say I have integers such as 2^0=1, 2^1=2, 2^2=4, 2^3=8, 2^4=16, 2^5=32
now when I get sum 42 of some integers belonging to above mentioned integer (the integers
cannot repeat itself i.e 2+2 cannot be done )
How can I obtain the actual integers participated in calculating the value 42 which is 32,8,2
Format your number as binary notation, in this case for 42 it would be 101010, from the notation get the index of 1 (start from 0) , it's 5,3,1. and then you have 2^5 + 2^3 + 2^1

integer divison with negative remainder

I'm trying to find a name for mathematical division operation which can produce negative reminder.
Examples of expected:
5 %? 2 = 2*2 + 1
11 %? 3 = 4*3 - 1
module of reminder should be as small as possible.
Does someone knew the name of such operation?
Computer language remainder functions return a positive remainder. What you need is simple. In your second example you want is a remainder of -1.
11 %? 3 = 4*3 - 1
But what you get is a remainder of 2.
11 %? 3 = 3*3 + 2
As I said in the comments, check the remainder 2 against half the divisor 3/2 = 1.5. Since the remainder is greater than half the divisor, subtract the divisor:
2 - 3 = -1
Yes, you will have to store a real number for half the divisor.

Is there a closed form available for the following table?

Below is a table which has a recursive relation as current cell value is the sum of the upper and left cell.
I want to find the odd positions for any given row denoted by v(x) as represented in the first column.
Currently, I am maintaining two one arrays which I update with new sum values and literally checking if each positions value is odd or even.
Is there a closed form that exists which would allow me to directly say what are the odd positions available (say, for the 4th row, in which case it should tell me that p1 and p4 are the odd places).
Since it is following a particular pattern I feel very certain that a closed form should exist which would mathematically tell me the positions rather than calculating each value and checking it.
The numbers that you're looking at are the numbers in Pascal's triangle, just rotated ninety degrees. You more typically see it written out like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
...
You're cutting Pascal's triangle along diagonal stripes going down the left (or right, depending on your perspective) strips, and the question you're asking is how to find the positions of the odd numbers in each stripe.
There's a mathematical result called Lucas's theorem which is useful for determining whether a given entry in Pascal's triangle is even or odd. The entry in row m, column n of Pascal's triangle is given by (m choose n), and Lucas's theorem says that (m choose n) mod 2 (1 if the number is odd, 0 otherwise) can be found by comparing the bits of m and n. If n has a bit that's set in a position where m doesn't have that bit set, then (m choose n) is even. Otherwise, (m choose n) is odd.
As an example, let's try (5 choose 3). The bits in 5 are 101. The bits in 3 are 011. Since the 2's bit of 3 is set and the 2's bit of 5 is not set, the quantity (5 choose 3) should be even. And since (5 choose 3) = 10, we see that this is indeed the case!
In pseudocode using relational operators, you essentially want the following:
if ((~m & n) != 0) {
// Row m, entry n is even
} else {
// Row m, entry n is odd.
}

Convert binary, octal, hexa FLOATS to decimal "manually" (ex: [135.263]B10 -> [?]B2)

I'm a student in programmation and I have a course called "Informatic Mathematics". In the exercices it's asked to convert floating numbers from decimal, octal, hexadecimal or binary to another base (not necesserly to base 10) and keep 12 digits after the comma(or the dot) if it is possible. For exemple:
(135.263)b10 => b2
(100101001.11)b2 => b10
(1011110010.0101)b2 => b8
...
I know how to convert numbers. The way I convert the decimal part (after the dot) is to divide this part by the highest multiple of the target base until I get 0 or until I reach the 12th digits after the dot. The problem is that I don't know all the negate multiples of 2 so usually I write them on a separate sheet, but usually I don't have to keep 12 digits after the dot and writing these multiples on a seperate sheet takes time and during the exam, time is a precious thing and I can't waste it to write these multiples.
So I would like to know if there's a better way to do these conversions or if anyone has any tips.
Also, when I convert from non-decimal number to another non-decimal number (ex: b2 => b8) I usually convert the first number to base 10 and then convert the base 10 number to the target base. I would like to know if there's a way to convert the first number directly into the target base without having to convert it in base 10 first.
BTW: Sorry if my english is a bit weird. I'm a french canadian and I did my best, but please let me know if there is something you do not understand well.
I'll start with b2 > b8.
001 011 110 010.010 100
As you see, I've separated the number into 3 digit segments (2^3 = 8). You have to add extra 0 to the left and to the right to make it like that. Then you convert it digit by digit. In this case you'll receive 1352.24
b2 => b10
Some harder math here. Mark digits in your number this way:
1 0 0 1 0 1 0 0 1 . 1 1
8 7 6 5 4 3 2 1 0 -1 -2
Then calculate fractional and whole part
2^0 + 2^3 + 2^5 + 2^8 + 2^-1 + 2^-2
b10 => b2
Multiple the fraction by 2 till you get 1. From each multiplication you take the whole part. Example:
0.25 * 2 = 0.5; 0.5 * 2 = 1;
Thus, 0.25 is 0.01;
UPD For negative conversions check out first and second complement.

Random number generation - constrained sequence

I'm trying to produce a set of 480 random integers between 1-9. However there are some constraints:
the sequence cannot contain 2 duplicate digits in a row.
the sequence must include exactly 4 sequences of odd numbers and 4 sequences of even numbers (in any order) within every 80 digit sequence (e.g. 6 4 5 2 4 8 3 4 6 9 1 5 4 6 1).
I have been able to produce a set of random numbers hat allows repeated digits, using:
NumRep <- sample(1:9, 480, replace=T)
but not been able to work out how to allow digits to be repeated over the entire set, but to not allow sequential repeats (e.g. 2 5 3 would be okay, 2 5 5 would not). I have got nowhere with the odd/even constraint.
For context, this is not homework! I am a researcher, and this is part of a psychological experiment that I am creating.
Any help would be greatly appreciated!
First, the problem loses the "random" way of simulating with that conditions. Anyway this code corresponds with the first constraint:
# Build a vector
C<-vector()
# Length of the vector
n<-480
# The first element
C<-sample(1:9,1)
# Complete the rest
for (i in 2:n){
# Take a random number not equal to the previous one
C[i] <- sample(c(1:9)[1:9!=C[i-1]],1)
}
# It is an odd number?
C %% 2 == 0
# How many consecutive odd numbers are in the sequence?
# Build a table with this information
TAB <- rle( C %% 2 == 0)
# Maximum of consecutive odd numbers
max(TAB$lengths[TAB$values==T])
# Maximum of consecutive even numbers
max(TAB$lengths[TAB$values==F])
I don't understand the second constraint, but I hope the final part of the code helps. You should use that information in order to interchange some values.

Resources