打印前 n 个自然数的正方形

原文:

给定一个自然数“n”,不使用、/和-打印前 n 个自然数的平方。 例:*

input:  n = 5
output: 0 1 4 9 16
input:  n = 6
output: 0 1 4 9 16 25

强烈建议尽量减少浏览器,先自己试试这个。 方法一:思路是利用之前的平方值计算下一个平方。考虑以下 x 的平方和(x-1)之间的关系。我们知道(x-1)的平方是(x-1)2–2 * x 1。我们可以把 x 2 写成

x2 = (x-1)2   2*x - 1 
x2 = (x-1)2   x   (x - 1)

在编写迭代程序时,我们可以跟踪 x 的先前值,并将 x 的当前值和先前值加到 square 的当前值上。这样我们甚至不用“-”运算符。 以下是上述方法的实施:

c

// c   program to print squares of first 'n' natural numbers
// without using *, / and -
#include
using namespace std;
void printsquares(int n)
{
    // initialize 'square' and previous value of 'x'
    int square = 0, prev_x = 0;
    // calculate and print squares
    for (int x = 0; x < n; x  )
    {
        // update value of square using previous value
        square = (square   x   prev_x);
        // print square and update prev for next iteration
        cout << square << " ";
        prev_x = x;
    }
}
// driver program to test above function
int main()
{
   int n = 5;
   printsquares(n);
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print squares
// of first 'n' natural numbers
// without using *, / and
import java.io.*;
class gfg
{
static void printsquares(int n)
{
    // initialize 'square' and
    // previous value of 'x'
    int square = 0, prev_x = 0;
    // calculate and
    // print squares
    for (int x = 0; x < n; x  )
    {
        // update value of square
        // using previous value
        square = (square   x   prev_x);
        // print square and update
        // prev for next iteration
        system.out.print( square   " ");
        prev_x = x;
    }
}
// driver code
public static void main (string[] args)
{
    int n = 5;
    printsquares(n);
}
}
// this code is contributed
// by akt_mit

python 3

# python 3 program to print squares of first
# 'n' natural numbers without using *, / and -
def printsquares(n):
    # initialize 'square' and previous
    # value of 'x'
    square = 0; prev_x = 0;
    # calculate and print squares
    for x in range(0, n):
        # update value of square using
        # previous value
        square = (square   x   prev_x)
        # print square and update prev 
        # for next iteration
        print(square, end = " ")
        prev_x = x
# driver code
n = 5;
printsquares(n);
# this code is contributed
# by akanksha rai

c

// c#  program to print squares
// of first 'n' natural numbers
// without using *, / and
using system;
public class gfg{
    static void printsquares(int n)
{
    // initialize 'square' and
    // previous value of 'x'
    int square = 0, prev_x = 0;
    // calculate and
    // print squares
    for (int x = 0; x < n; x  )
    {
        // update value of square
        // using previous value
        square = (square   x   prev_x);
        // print square and update
        // prev for next iteration
        console.write( square   " ");
        prev_x = x;
    }
}
// driver code
    static public void main (){
        int n = 5;
        printsquares(n);
    }
}
// this code is contributed
// by ajit

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

0 1 4 9 16

时间复杂度:0(n)

辅助空间:0(1)

方法二:前 n 个奇数之和是从 1 到 n 的自然数的平方,比如 1,1 3,1 3 5,1 3 5 7,1 3 5 7 9,…。 以下是基于上述概念的程序。感谢 aadithya umashanker 和 raviteja 提出这种方法。

c

// c   program to print squares of first 'n' natural numbers
// without using *, / and -
#include
using namespace std;
void printsquares(int n)
{
    // initialize 'square' and first odd number
    int square = 0, odd = 1;
    // calculate and print squares
    for (int x = 0; x < n; x  )
    {
        // print square
        cout << square << " ";
        // update 'square' and 'odd'
        square = square   odd;
        odd = odd   2;
    }
}
// driver program to test above function
int main()
{
   int n = 5;
   printsquares(n);
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print
// squares of first 'n'
// natural numbers without
// using *, / and -
import java.io.*;
class gfg
{
static void printsquares(int n)
{
    // initialize 'square'
    // and first odd number
    int square = 0, odd = 1;
    // calculate and
    // print squares
    for (int x = 0; x < n; x  )
    {
        // print square
        system.out.print(square  
                           " " );
        // update 'square'
        // and 'odd'
        square = square   odd;
        odd = odd   2;
    }
}
// driver code
public static void main (string[] args)
{
    int n = 5;
    printsquares(n);
}
}
// this code is contributed
// by ajit

python 3

# python3 program to print squares
# of first 'n' natural numbers
# without using *, / and -
def printsquares(n):
    # initialize 'square' and
    # first odd number
    square = 0
    odd = 1
    # calculate and print squares
    for x in range(0 , n):
        # print square
        print(square, end= " ")
        # update 'square' and 'odd'
        square = square   odd
        odd = odd   2
# driver code
n = 5;
printsquares(n)
# this code is contributed
# by rajput-ji

c

// c# program to print squares of first 'n'
// natural numbers without using *, / and -
using system;
class gfg
{
static void printsquares(int n)
{
    // initialize 'square'
    // and first odd number
    int square = 0, odd = 1;
    // calculate and
    // print squares
    for (int x = 0; x < n; x  )
    {
        // print square
        console.write(square   " " );
        // update 'square'
        // and 'odd'
        square = square   odd;
        odd = odd   2;
    }
}
// driver code
public static void main ()
{
    int n = 5;
    printsquares(n);
}
}
// this code is contributed
// by inder_verma..

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

0 1 4 9 16

时间复杂度:0(n)

辅助空间:0(1)

本文由沙钦供稿。如果你发现任何不正确的地方,请写评论,或者你想分享更多关于上面讨论的话题的信息