给定自然数’n’,不使用*,/和-打印前n个自然数的平方。
例子 :
Input: n = 5
Output: 0 1 4 9 16
Input: n = 6
Output: 0 1 4 9 16 25
强烈建议您最小化浏览器,然后自己尝试。
方法1:想法是使用先前的平方值计算下一个平方。考虑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
// wothout 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
// wothout 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
// wothout 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
PHP
Javascript
C++
// C++ program to print squares of first 'n' natural numbers
// wothout 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
Python3
# Python3 program to print squares
# of first 'n' natural numbers
# wothout 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..
PHP
Javascript
输出:
0 1 4 9 16
方法2:前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
// wothout 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
Python3
# Python3 program to print squares
# of first 'n' natural numbers
# wothout 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..
的PHP
Java脚本
输出 :
0 1 4 9 16