Regular polygon using only 1 s in a binary numbered circle | method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver code to test above methods
function checkPolygonWithMidpoints ( arr , N , midpoints ) { for ( let j = 0 ; j < midpoints ; j ++ ) { let val = 1 ; for ( let k = j ; k < N ; k += midpoints ) { val &= arr [ k ] ; } if ( val && parseInt ( N / midpoints ) > 2 ) { document . write ( " " + parseInt ( N / midpoints ) + " " ) ; return true ; } } return false ; } function isPolygonPossible ( arr , N ) { let limit = Math . sqrt ( N ) ; for ( let i = 1 ; i <= limit ; i ++ ) { if ( N % i == 0 ) { if ( checkPolygonWithMidpoints ( arr , N , i ) || checkPolygonWithMidpoints ( arr , N , parseInt ( N / i ) ) ) return ; } } document . write ( " " ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ] ; let N = arr . length ; isPolygonPossible ( arr , N ) ;