آخرین مطالب

PHP

<?php
// PHP program to find number
// of squares in a chessboard
 
// Function to return
// count of squares;
function countSquares($n)
{
    // A better way to
    // write n*(n+1)*(2n+1)/6
    return ($n * ($n + 1) / 2) *
               (2 * $n + 1) / 3;
}
 
// Driver Code
$n = 4;
echo "Count of squares is " ,
            countSquares($n);
 
// This code is contributed
// by nitin mittal.
?>

Output:  

Count of squares is 30

Time Complexity: O(1)

Auxiliary Space: O(1)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Solve DSA problems on GfG Practice.

Solve Problems

[/membership]

پاسخ دهید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *