'Error in python finding coolness of the number

coolness of the number is defined as the number of "101"s occuring in its binary representation. If the number binary representation has "101" then it's coolness is 1 A number is defined as very cool if it's coolness is greater than or equal to k

input : 21 2 output: 1

but i'm getting

output:2 instead of 1

 def v(j):
       res=''
        c=0
      while j>0:
        res=res+str(j%2)
        j=j//2
        for i in range(2,len(res)):
            if res[i]+res[i-1]+res[i-2]=="101":
                c+=1
    return c>=k
n,k=map(int,input().split())
r=0
for i in range(2,n+1):
    if v(i):
        r=r+1
print(r)


Solution 1:[1]

The easiest way to calculate the "coolness" is to use built-in functions rather than to invent yours. Function bin(x) converts an integer number to a string with its binary representation. Method str.count() counts the substrings.

bin(21).count('101')
# 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1