Form coils in a matrix | Print coils in a matrix of size 4 n x 4 n ; Number of elements in each coil ; Let us fill elements in coil 1. ; First element of coil1 4 * n * 2 * n + 2 * n ; ; Fill remaining m - 1 elements in coil1 [ ] ; Fill elements of current step from down to up ; Next element from current element ; Fill elements of current step from up to down . ; get coil2 from coil1 ; Print both coils ; Driver code
< ? php function printCoils ( $ n ) { $ m = 8 * $ n * $ n ; $ coil1 = array ( ) ; $ coil1 [ 0 ] = 8 * $ n * $ n + 2 * $ n ; $ curr = $ coil1 [ 0 ] ; $ nflg = 1 ; $ step = 2 ; $ index = 1 ; while ( $ index < $ m ) { for ( $ i = 0 ; $ i < $ step ; $ i ++ ) { $ curr = $ coil1 [ $ index ++ ] = ( $ curr - 4 * $ n * $ nflg ) ; if ( $ index >= $ m ) break ; } if ( $ index >= $ m ) break ; for ( $ i = 0 ; $ i < $ step ; $ i ++ ) { $ curr = $ coil1 [ $ index ++ ] = $ curr + $ nflg ; if ( $ index >= $ m ) break ; } $ nflg = $ nflg * ( -1 ) ; $ step += 2 ; } $ coil2 = array ( ) ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) $ coil2 [ $ i ] = 16 * $ n * $ n + 1 - $ coil1 [ $ i ] ; echo " Coil ▁ 1 ▁ : ▁ " ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) echo $ coil1 [ $ i ] , " ▁ " ; echo " Coil 2 : " ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) echo $ coil2 [ $ i ] , " ▁ " ; } $ n = 1 ; printCoils ( $ n ) ; ? >