• Register
1 vote
81 views

Problem:

I wrote a program in python to check if a given number is sparse or not. So I wrote this code:
 

def IsSparse(n): 
  
     
    if ((n<<1) & (n>>1)): 
        return 0
   
    return 1

print(IsSparse(35)) 
print(IsSparse(16)) 
print(IsSparse(12)) 
print(IsSparse(72))

And when I run this or when I compile this it shows a wrong result. The output is:

1
1
1
1

But the actual output should be:

0
1
0
1

What should I do to get the actual result?

Thanks in advance.

8 6 2
4,980 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

A number is said to be a sparse number if in binary representation of the number no two or more consecutive bits are set. Write a function to check if a given number is Sparse or not.

So you can try out this code to get the actual result

def IsSparse(n): 
  
     
    if (n & (n>>1)): 
        return 0
   
    return 1

print(IsSparse(35)) 
print(IsSparse(16)) 
print(IsSparse(12)) 
print(IsSparse(72))

And its output is:
 

0
1
0
1

Hope this should help you. If you don not understand anything please feel free to ask me any question you want to know.

Happy codng.

11 6 4
34,950 points

Related questions

0 votes
1 answer 12 views
12 views
Problem: Is there any clue to solve this error: Check if a number is prime or not in C++?
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
2 answers 32 views
0 votes
1 answer 7 views
7 views
Problem: Is there anyone to helo on > 'how to check if a number is even' ?
asked Mar 17 sadi1982 36.3k points
0 votes
1 answer 6 views
6 views
Problem: I have a problem.How can I fix this java check if character is number?
asked Mar 29 Madhav96 2.3k points
0 votes
1 answer 97 views
97 views
Problem: I want to know whether the character at a certain index in a string is a number or not in C++. Moreover, I wanna see whether the user entered a number as input to a character variable or some other character. How can I do that?
asked Oct 21, 2020 Code Learner 9.9k points
0 votes
1 answer 12 views
12 views
Problem: Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like &sup2; and &frac34; are also considered to be numeric values. then how check if a string is a number python
asked Feb 24 Mashhoodch 13k points
0 votes
1 answer 13 views
13 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 24 shamimkhan 15.1k points
1 vote
1 answer 335 views
335 views
Problem: I wrote a program in python to check if two numbers are equal without using arithmetic and comparison operators. The code I wrote is: def Same(a, b): while (a & b != 0): summon = a & b a = a ^ b b = summon << 1 return a if Same( ... . So, I can not check if two numbers are equal without using arithmetic and comparison operators. Can anyone provide me any code of this. Thanks in advance.
asked Apr 22, 2020 samhaz 5k points
0 votes
1 answer 15 views
15 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 sadi1982 36.3k points
0 votes
1 answer 16 views
16 views
Problem: I am stuck during learning as I have already mentioned in my question someone please cooperate me to continue my project-Thanks
asked Mar 23 shamimkhan 15.1k points