Sum of LCM range from 1 to 10^9 [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So, first programming course, first nongraded assignment:
In C++, for the range of numbers from 1 to 1 billion, find the sum of the numbers that are divisible (without remainder) by all 1 through 99. How do I go about this? How do I even find the lowest number divisible by 1:99?
edit: this isn't hw to be turned in, just something to think about. I would try some type of vectorizing in matlab, but this is my first day trying c++ so I really have no idea, I just learned how to initialize a variable.

// In pseudocode a very basic algorithm:
main
for i: 1 to 1000000000
if (TestValue(i))
Output(i)
TestValue(i)
for j: 1 to 99
if j does not divide i evenly
return false
return true
Of course, this won't be very performant. You might notice that if a number is evenly divisible by all numbers between 1 and 99, then it must be divisible by the set of prime factors in 1..99. For instance, in 1..19 the prime factors are: 2, 2, 2, 2, 3, 3, 5, 7, 11, 13, 17, 19. If something is evenly divisible by all numbers 1..19 then it must be evenly divisible by 2*2*2*2*3*3*5*7*11*13*17*19 = 232792560. To find all the numbers between 1 and 1000000000 that are evenly divisible by 1..19, I would find all the numbers between 1 and 1000000000 that are evenly divisible by 232792560 instead.

Related

Longest subsequence whose sum is divisible by k [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am practicing some Dynamic Programming problems and encounter this problem
Given an array of n(1<=n<=1000) integers and a positive integer k(k<=1000). Find the longest subsequence whose sum is divisible by k.
For example, a = [1,6,11,5,10,15,20,2,4,9] and k=5.
The result should be: [9,4,20,15,10,5,11,6] because 9+4+20+15+10+5+11+6 = 80, which is divisible by 5.
What is a suitable approach to solve this problem?
Start with the longest possible subsequence, the array itself. Calculate its sum modulo k. If its zero, we are done. Otherwise, find a number such that its modulo k is the same. If that exists, remove it and we are done. Otherwise keep going.
Brute Force Approach:
We can generate all the possible sub-sequences and then find the largest sub-sequence among them whose sum is divisible by K.
However, the time complexity of this approach will be O(n*n).
Efficient Approach:
We can use dynamic programming here. Kindly note that this approach will only work for small values of K.
dp[i][curr_mod] = max(dp[i + 1][curr_mod], dp[i + 1][(curr_mod + arr[i]) % m] + 1)
Here, dp[i][curr_mod] stores the longest subsequence of subarray arr[i…N-1] such that the sum of this subsequence and curr_mod is divisible by K.
At each step, either index i can be chosen to update curr_mod or it can be ignored.
Also, note that only SUM % m needs to be stored instead of the entire sum as this information is sufficient to complete the states of DP.

How to count in binary in base 10 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I would like an integer in c++ to count up in binary like:
1, 10, 11, 100, 101, 110, 111
But i want to values to be in base 10 like:
one, ten, eleven, one hundred, one hundred and one, etc.
Basically count up in decimal but look like base 10.
Is there a algorithm to do this.
-Joseph
This is how you should proceed:
use a string for binary values to display it
you should implement a cycle which will end when your number reaches 0 and should write the result of currentValue % 2 to the start of your string then divide your number with 2
you should implement a function which will return the humanely readable number text, you might want to take inspiration from here, but a clue is that the digit number % 3 is relevant. If it is 0, then you will need to write the digit if the digit next to it to the left does not happen to be one, so values between 11-19 are an exception here and don't forget thousand, million, billion, if the modulo is 1, then you will have a special text, like eleven, ... nineteen, twenty, ..., ninety and if modulo is 2, then the text will be the digit + "hundred"
You will basically have a number and will be able to get the binary from it and the textual representation as well.
String is not necessary for the algorithm, one can cout a std::bitset as well.

Find how many numbers meet the constraints in a range

Given 2 integers l and r, calculate how many numbers in [l, r] that meet these constraints
1) The number should be divisible by 7
2) The number contains at least three digit 7
3) The number contains more digit 7 than digit 4
777, 774746 meet those constraints, 7771, 77, 747474 are not.
Using brute force can easily find the answer but when the range is very large then it might take a lot of time.
I think dynamic programming can help to solve this problem, but i can't think of the solution
Can someone give me some guide?
Taking from the original brute-force version:
Iterate with i over numbers between [l,r]
Use modulo to check if i is divisible by 7
Use modulo and division to get counts of digits in i
digit_count(7) >= 3
digit_count(7) > digit_count(4)
here's some ideas I came up with...
1. Use only multiples of 7, implicitly fulfilling the first criterion:
This one is really simple. We can improve this to only use i that is divisible by 7. If I give you a number x and ask you to generate me numbers divisible by n until you reach y, then you'd best do:
for (auto i = x + x % n; i < y; i += n)
So for the case of multiples of 7 between l and r, all you need to do is run the loop for (auto i = l + l % 7; i < r; i += 7) This will give you 7x speed-up from the brute-force version.
2. Remember the digit counts
There's no need to execute numerous divisions and modulos to get you the count of digits of each number you go through. Since you know by how much you increment, you also know what digits change to what. This way, you'd only need to split into digits the starting number (e.g. l % 7 + l).
Now what we'd be storing isn't the count of digits but actually something very much resembling BCDs - an array of digits that represent the number we are currently working with. You'd then get something like std::vector<int> expressing the array of [7, 7, 2, 4, 5, 7] for number 772457. Now all you need to do is to use the BCD arithmetic inside the array every time you increment the loop counter, going [7, 7, 2, 4, 5, 7] + 6 = [7, 7, 2, 4, 6, 3].
The other thing we'd need to store are two ints - sevens and fours. In the intitialization phase, once you "disintegrate" the first number into the array, you'd just go through it and increment sevens for each 7, fours for each 4. And you'd just keep this numbers up to date: with each update of the array, you'd decrement fours for each 4 you took away and increment it for each 4 you created in the array. And the same for number 7. You can then compare sevens >= 3 && sevens > fours and know the result fast.
Funny thing is, that this gives you no theoretical improvement in the complexity and it might not work, but I suspect it should... It's quite a lot of code so I'm not going to provide it. You might end up working with the BCD array inverted or starting with the r end of iteration range so you don't need to resize the array. And maybe you can come up with many more improvements and tweaks. However, I have strong doubt that the solution can be made asymptotically less complex this way.
3. More thoughts
Now this wasn't dynamic programming at all. Or was it? If you think about it, I have a gut feeling that this idea of an array of numbers as BCD can now be converted to a problem where you look for permutations containing a given combination. You can make a graph out of it and search it. And that's where you'd go dynamic. I'm afraid, however, that this would make for quite a longer post...
But I already got the first doubt about that and that's the check for divisibility by 7 which would then be applied to all the numbers that are found in the graph (the graph would only support criterions 2 and 3 by its nature and yield all numbers containing the combinations). So in the end, it boils down to sizes of ranges that should be supported by the alrgorithm and the ratio of numbers fulfilling the first criterion and the numbers fulfilling the second and third ones in those ranges.
EDIT:
I have since found that my idea of computing the count of numbers fitting the criteria is incorrect. Some small comparisons table:
| range | numbers f/ c2 | c2_groups | c2_total | c1_total |
| 0 - 1k | 777 | 1 | 1 | ~143 |
| 1k - 10k | _777, 7_77, 77_7, 777_ | 4 | 40 | ~1286 |
| 10k - 100k | __777, _7_77, ... | 10 | 1000 | ~12857 |
Where numbers f/ c2 are numbers fulfilling criterion 2, c2_groups is count of possible combinations of any digit and 7s in the number, cx_total is total count of numbers fulfilling criterion x in the range.
Having that, it looks like it's quite questionable whether it would be efficient to filter by the number of digits criteria first. I suppose that would require some mathematical analysis that would take longer than implement the solution...
Space search
With having state equivalent to method #2, it is possible to do DFS in the numbers range. Instead of incrementing by 7, it would store a digits vector and increment values in it based on an offset that would be movable, e.g.
increment [1, 0, 7, _] -> [1, 0, 8, _]
^ ^
This is what the algorithm will be doing in the core loop. You can then check whether the current digits vector setup can fulfill the criteria - e.g. [0, p, _, _] can fulfill them, while [0, 0, p, _] cannot (p is the element that is being pointed to). This way, you will keep incrementing the highest possible digit, skipping a lot of numbers. Every time there is a possibility to fulfill the requirements, you will increment the offset and repeat the process:
push [7, 7, _, _] -> [7, 7, 0, _]
^ ^
Once you're at the least significant digit position, you'll also start checking the divisibility by 7 of each candidate. You can try either converting the digits to int and using modulo or using some sort of divisibility algorithm (these use digits so that's a pleasant coincidence).
This way, you'll get a number that passes all criteria and return it. Now you might come to a situation where you exhaust all the digits in given digit range. In that case, you need to move the offset one place back:
pop [7, 7, 7, 9] -> [7, 7, 7, _]
^ ^
Now, you'd use increment, see that [7, 7, 8, _] can fulfill the criteria and push again. Then run through 0, 1, 2, ... sequence until you come to 7, see that 7787 is ok with both 2nd and 3rd criteria but fails division by 7. And so on...
You'll also need to check whether you're not already over the r limit. I guess that can be done in quite a sane manner by splitting r to digits as well and comparing it from the most significant digit.
Given that we have no math analysis for this, and that this is still going through quite a lot of numbers (especially in case that 7 is the least significant digit), I wonder whether this is really worth implementing. But it's not something super-complex either. Good luck!
For 1: if(yourint % 7 == 0)
For 2: check this link split int into digits, check if digit equals 7 and count to 3.
For 3: expend link 2 with an if a digit equals 7 or 4 than counter++
At the end you should check your counters (7 an 4) which one is the highest.

Using Dynamic Programming To Group Numbers

Let's say you have a group of numbers. You need to eliminate numbers until there is only one left. This is hard to explain, so let me provide you with an example.
The numbers are 3, 6, 9, and 10.
You pair 3 with 9. You eliminate 3. (Note: either one of them could be eliminated). Now there are 6, 9, and 10 left. You pair 6 with 9. You eliminate 9. Now there are 6 and 10 left. You pair 6 with 10 (only option).
The problem is: I want to find the maximum value obtained from this elimination. Each time a number is eliminated, the XOR value of those two numbers is added to the count. In the previous example, the total value would be (3 ^ 6) + (6 ^ 9) + (6 ^ 10) = 10 + 15 + 12 = 37. This happens to be the maximum value that can be obtained from any elimination combination.
How would I solve this problem in Java with 2000 numbers? I know I can find every possible combination using brute force, but the run time of this was more than two seconds, and I prefer my solutions to be under two seconds. The only option left is Dynamic Programming.
Does anyone know how to solve this with Dynamic Programming?

Efficient algorithm to calculate all possible pair whose multiplication is a perfect quare [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two numbers N and M.
I efficiently want to calculate how many pairs of a,b are there such that 1<=a<=N and 1<=b<=M and a*b is a perfect square.
I know the obvious N*M algorithm to compute this. But i want something better than that.
Thanks for any help in advance. A pseudo code will be more helpful.
EDIT : I think it can be done in a better time may O(m+n) or something like that but calculating new pairs directly from previous pairs rather than iterating over all a and b.
My approach would be this:
for s is quare and s <= N*M
Do a prime factorization of s.
iterate over the partitions of this prime factorization and check which ones fullfill your requirement
Iterating over the possible partitions may be a bit tricky, but I'm quite certain that this is the most efficient approach that is possible.
Iterating over square numbers, on the other hand, is trivial:
for(int i = 0, square = 0; /*whatever*/; square += 2*i++ + 1)
I would go for a way using prime decompositions.
Get a hold of all the prime numbers between 1 and max(N,M), and let us call them the (p0, p1, ... pn)
Then any number a <= N and b <= M can be written as and , for i from 1 to n, where the ai and bi can be any positive integer or 0.
Then, any product a*b can be written as , and the caracterization of a perfect square in that writing would be that all the (ai+bi) need to be even (0 is even, for the record).
Then you somehow need to iterate over all the (ai) such that a <= N, and for each set of (ai) generate all the (bi) such that b <= M and all the (ai+bi) are even.
Not sure that's the anywhere near efficient, but should work just fine.