I am trying to find out the numbers between 1 and 1,000 that the sum of their digits is equal to 3. I am just looking for any formula that can help me calculate this. For e.g. 111, or 12 equals to 3, the ones, tens, and hundreds added up together will equal 3.
any help will be appreciated.
think of all numbers having 3 digits: 001 and 002
We can start to iterate on this quickly. You cannot have a number in that sequence greater than 3.
So immediately we rule out all numbers >= 400
We can also rule out,within each group of hundred, any number that is greater than ?40 (eg 140 340)
Then we can start to just press into the numbers a bit.
We only have to dig into ?01 - ?39 for 0, 1, 2 and 3.
Start with 00?. We know that there is only one number works here : 0 + 0 + x =3 solve : 003
So we have 0, moving up to the next set of 10 : 01? we know there is only one number that will work. 012.
We have logic, each leading two digit combination leads to only one solution. We know we only have 0?? 1?? 2?? and 3?? for the leading digit. We have ?0? ?1? ?2? and ?3? for the second digit.
We can be comfortable listing : 3, 12, 21, 30, 102, 111, 120, x13?, 201, 210, x22?, 300
If you don't want to use math, use python:
a=[]
for x in range(10):
for y in range(10):
for z in range(10):
if x+y+z==3:
a.append('%r%r%r'%(x,y,z))
a = ['003','012','021','030','102','111','120','201','210','300]
Related
Description of the Problem:
I have an array of two-digit numbers going from 00 to 99. I must choose a number at random anywhere in the array; let's call this result r. I may now take up to n steps within the array to "travel" to another number in the array, according to the following rules:
Adding or subtracting 1 from r takes one (1) step; I cannot add 1 if there is a 9 in the ones place (ex: 09, 19, 29, ...) and I cannot subtract 1 if there is a 0 in the ones place (ex: 00, 10, 20, ...)
Adding or subtracting 10 from r takes one (1) step; I cannot bring the result to lower than 00 or higher than 99.
By taking two (2) steps, I can swap the digits in the ones and tens place (ex: 13 -> 31, 72 -> 27); however, I can't perform the swap if the digits are the same (ex: can't swap 00, 11, 22, ...)
For a given number x (00 <= x <= 99) I want to count the set of unique values of r from which I can travel to x, given that I can take between 0 and n steps. I call this count how "accessible" x is. I'd like to express this as a formula, A(x, n), rather than just brute-forcing the results for each combination of x and n.
What I Have Tried:
A(x, 0) is easy enough to calculate: A(x, 0) = 1 for all values in the array, because the only way to reach x from r is for r = x; I take zero (0) steps to reach it.
A(x, 1) is trickier, but still simple: you just take into account the new paths available if I spend my one step on either Rule #1 or Rule #2, and add them to A(x, 0). A(x, 2) is where I have to start including Rule #3, but also includes the problem of backtracking. For instance, if I want to reach x and x = r, and I have two (2) steps available, I could perform the following operation: Step 1, r -> r' = r+1 (Rule #1); Step 2, r' -> r'' = r'-1 (Rule #1); r'' = x AND r'' = r. This does not add to my count of unique values from which I can travel to x from.
Where I am Stuck:
I cannot figure out how to count the number of backtracking paths in order to remove them from the otherwise simple calculations of A(x, n), so my values of accessibility are coming out too high.
I'm having trouble finding a transformation that adheres to the following criteria:
The 3 input numbers can be negative, positive, or 0
The scale is unknown for a single trio of numbers, however, I could scan through the data to find the min and max values for the entire data-set, if that helps.
The 3 output numbers must be between 0 and 100 (inclusive) and sum up to 150, with 50 being the 'new zero'. So for 3 input numbers of 0, 0, 0, the output would be 50, 50, 50. Negative numbers would end up below 50 (but >= 0), and positive numbers would end up above 50 (but <= 100).
Here are two real examples of input numbers:
-19, 253, -267
-479, 137, -179
Your help would be appreciated.
I have a list of numbers a1, a2, a3, a4, a5, ... and so on. If we find XOR of all subsets then I noticed that frequency of each distinct XOR is the same.
Example 1
# list of numbers
[2, 0, 9]
# XOR of all subsets including empty set
XOR of () = 0
XOR of (2,) = 2
XOR of (0,) = 0
XOR of (9,) = 9
XOR of (2, 0) = 2
XOR of (2, 9) = 11
XOR of (0, 9) = 9
XOR of (2, 0, 9) = 11
# Frequency of each XOR value
{0: 2, 2: 2, 9: 2, 11: 2}
In the above example, we can see that XOR values 0, 2, 9, 11 have the same frequency that is 2
Example 2
It doesn't matter what is the size of the list, whether elements are repeated or not, the property seems to hold
# list of numbers
[2, 0, 9, 9, 9, 45, 1, 2, 1, 1]
# skipping showing the subsets
# Frequency of each XOR value
{0: 64,
2: 64,
9: 64,
45: 64,
1: 64,
11: 64,
47: 64,
3: 64,
36: 64,
8: 64,
44: 64,
38: 64,
10: 64,
46: 64,
37: 64,
39: 64}
First of all, I am not sure whether it will always hold. I have tested it for quite a few numbers of examples and it seems to work for all of them. There are a lot of XOR questions asked on StackOverflow but I didn't find anything related to this question.
Can someone please help with the following?
Whether this property will always hold?
If no, can you share 1 example?
If yes, can you please answer why?
Assuming you know about the linear combination of basis elements.,
Let ai1,ai2,ai3,....aik be the basis elements for the given array.
We get 2k unique xor values by spanning above set.
Let that set be X = { x1,x2,.......x2k }
Now coming to remaining (n-k) non basis elements,
There are 2(n-k) subsets among them.
let the set of xor of each subsets be D = { d1,d2,....d2n-k }
Note: ∀di∈X
proof:
We know that, Each di is xor of(linear combination of) some subset of non basis elements, and a non basis elements itself linearly representable using basis elements.(ie:can be formed by xor-ing some subset of basis elements).
Therefore, each di is linearly representable with basis element, thus it belongs to set X.
Now coming to the original question, let A be given array.
Then set of subset of A = X×D.
Because X is set of subset of some k distinct element of array, and D is set of subset of remaining n-k elements., So merging them with all combinations gives set of subset of original array
Claim: ∀i, Li=di⊕X produces the same set, actually the set X itself, but just in different order.
proof:
For any two different integer p,q : we have p⊕u≠q⊕u
Since all x1,x2...... are distinct.
Here di is similar to u in above equation.
so Li = di⊕X = di⊕{x1,x2,......} produces a another unique set of numbers.
ie: lij ≠ lik for j≠k
Now we are left with proving that each Li is X itself.
proof:
The value of Each lij∈Li cannot have any new value apart that span of basis, as lij = di⊕xj, here what we did is only linear combination within basis.
So all lij still belongs to span of basis(and distinct). so, the Li is X.
Now the size of D is 2^(n-k), and ∀i, di×X = X
hence the same set of xor values in X is repeated 2^(n-k) times.
ie: each element in X (span of basis elements) is repeated 2^(n-k) times.
Where k is number of basis elements.
Write a program that reads a series of numbers, ending with 0, and then tells you how
many numbers you have keyed in (other than the last 0). For example, if you keyed in
the numbers 5, -10, 50, 22, -945, 12, 0 it would output ‘You have entered 6 numbers.’.
doing my homework and can get this one to work
what stumps me is i understand adding the numbers to get the sum total but what do i call the number of numbers ...
thanks
Python has a very simple function that could be used here, string.count(). Given each number is separated by a comma, you can count the amount of commas to get the amount of numbers (not including the 0, which doesn't have a comma after it). An example of this in use would be
input = 5, -10, 50, 22, -945, 12, 0
Number_of_Numbers = input.count(',')
So I need a way to figure out how to get 5 numbers, and when you add any 2 of them, it will result in a sum that you can only get by adding those specific two numbers.
Here's an example of what I'm talking about, but with 3 numbers:
1
3
5
1 + 3 = 4
1 + 5 = 6
3 + 5 = 8
Adding any two of those numbers will end up with a unique sum that cannot be found by adding any other pair of the numbers. I need to do this, but with 5 different numbers. And if you have a method of figuring out how to do this with any amount of numbers, sharing that would be appreciated as well.
Thank you
1, 10, 100, 10000, 100000 gives you five numbers like you desire.
In general, 1, 10, 100, 1000, ..., 10^k where k is the number of numbers that you need.
And even more general, you can say b^0, b^1, ..., b^k, where b >= 2. Note that you have the special property that not only are all the pairwise sums unique, but all the subset sums are unique (just look at representations in base b).
The set {1, 2, 5, 11, 21} also works.
You can start with a set of two or three elements that fit that property (any addition operation on two elements from the set {1,2,5} gives you an unique sum) and only include the next number being considered if additions of current elements and this new element also give you unique sums.
An example run-through:
Suppose our starting set S is S={1,2,5}. Let U be the set of all sums between two elements in S.
Elements in S give us unique sums 1+2=3, 1+5=6, 2+5=7, so U={3,6,7}.
Consider adding 11 to this set. We need to check that 1+11, 2+11, and 5+11 all give us sums that are not seen in U and are all unique among themselves.
1+11=12, 2+11=13, 5+11=17.
Since 12, 13, and 17 are all unique sums among themselves, and are not found in U, we can update S and U to be:
S1 = {1,2,5,11}
U1 = {3,6,7,12,13,17}.
You can do the same procedure for 21, and you should (hopefully) get:
S2 = {1,2,5,11,21}
U2 = {3,6,7,12,13,17,22,23,26,32}.
If all you need is a quick set though, the solution that Jason posted is a lot faster to produce.
1
2
4
8
16
1
3
9
27
81
suggests x ^ n where n is a member of a subset of Natural numbers