Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a row of bits:
001111000000000

And i want to flip all of these so that they're false, but I only have a set number of commands to do this, these commands look like this:
3, 5, 7, 11, 13, 17, 19 ... (prime numbers, excluding 2)

Each command flips the corresponding amount of bits using the NOT operator (0 becomes 1 and 1 becomes 0). My goal is to use the lowest amount of commands to achieve this.

Note that there is always enough space "after" the last 1 in our row of bits for us to complete the task.
Example Problem:
000000011000000000

The required commands that need to be issued in order for the table to be filled with 0's are the following:

STEP 1:
command: starting at index 7, type 5 (toggle 5 elements, starting from index 7)
resulting table: 000000000111000000

we still have 1's in the table, continue

STEP 1:
command: starting at index 9, type 3
resulting table: 000000000000000000

All elements of the table are false, we've completed the problem with 2 steps.

What I have tried:

I have tried using a simple min-max algorithm that starts at the first 1 and, using a couple conditions, decides on the best command to use. This only works in simple scenarios, and not in more complex ones.

To better describe what I've tried I'm including pseudocode of my attempt at solving this:

C++
primes[] = {3, 5, 7, 11, 13, 17 ... }
table[] = {0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0... }

bestFit(index)
    cwi = length of 1's without 0's starting at index
    
    if cwi == 0 then
        return 0

    for prime in primes do 
        difference = prime - cwi

        if difference == 0 then
           flipBits(index, cwi) // flip cwi bits starting from index
           return index + cwi

        else if difference == 3 then
           flipBits(index, cwi + 3)
           return bestFit(index + cwi)

    return 0



index = index of first 1

while !solved // solved = all elements are 0
    index = bestFit(index of first 1)
Posted
Updated 19-Jan-23 2:46am
v3
Comments
Graeme_Grant 17-Jan-23 10:42am    
Need to post code so that we can see what you are doing.
Patrice T 17-Jan-23 16:12pm    
Show our work so far.
and show how you solve 001111000000000
Graeme_Grant 19-Jan-23 19:25pm    
Why Pseudo-code and not your actual code? Are you expecting us to respond in kind?

1 solution

Binary: 00111100000 = Decimal: 480

Find all the factors of 480 - ie: numbers that would divide 480 without leaving any remainder. eg: 480/10 = 48; therefore, 10 is a factor of 480 and 48 is also a factor of 480.

Hence, the factors of 480 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 20, 24, 30, 32, 40, 48, 60, 80, 96, 120, 160, 240, 480.

Prime factors are: 2, 3, 5 - ie: 25 × 31 × 51 = 480.

That should be enough to point you in the right direction to code this.

UPDATE

These are the results for the two binary numbers in your question:
* For 001111000000000 (7,680) the prime factors are: 29 x 31 x 51
* For 001101100100001000000000000000000 (1,820,590,080) the prime factors are: 218 x 31 x 51 x 4631
 
Share this answer
 
v4
Comments
GalBrot 18-Jan-23 6:24am    
Hi, thanks for replying! I think there has been some miscommunication :D - firstly, I do not have access to a 2, only primes that follow after it, secondly, I'm looking for the lowest number of commands that are to be executed so that all values are false. I'll try to update the question so that it better describes the problem at hand.
Graeme_Grant 18-Jan-23 18:46pm    
As requested above, by myself and Patrice, you need to post your code if you want any more help. We are more than happy to point you in the right direction.
GalBrot 19-Jan-23 8:47am    
I apologize, I'm new to this platform and I haven't noticed the comments above. I've updated my question accordingly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900