Given a natural number ‘n’, print squares of first n natural numbers without using *, / and -.
Examples :
Input: n = 5
Output: 0 1 4 9 16
Input: n = 6
Output: 0 1 4 9 16 25
We strongly recommend to minimize the browser and try this yourself first.
Method 1: The idea is to calculate next square using previous square value. Consider the following relation between square of x and (x-1). We know square of (x-1) is (x-1)2 – 2*x + 1. We can write x2 as
x2 = (x-1)2 + 2*x - 1
x2 = (x-1)2 + x + (x - 1)
When writing an iterative program, we can keep track of previous value of x and add the current and previous values of x to current value of square. This way we don’t even use the ‘-‘ operator.
Below is the implementation of above approach:
C++
#include<iostream>
using namespace std;
void printSquares( int n)
{
int square = 0, prev_x = 0;
for ( int x = 0; x < n; x++)
{
square = (square + x + prev_x);
cout << square << " " ;
prev_x = x;
}
}
int main()
{
int n = 5;
printSquares(n);
}
|
Java
import java.io.*;
class GFG
{
static void printSquares( int n)
{
int square = 0 , prev_x = 0 ;
for ( int x = 0 ; x < n; x++)
{
square = (square + x + prev_x);
System.out.print( square + " " );
prev_x = x;
}
}
public static void main (String[] args)
{
int n = 5 ;
printSquares(n);
}
}
|
Python 3
def printSquares(n):
square = 0 ; prev_x = 0 ;
for x in range ( 0 , n):
square = (square + x + prev_x)
print (square, end = " " )
prev_x = x
n = 5 ;
printSquares(n);
|
C#
using System;
public class GFG{
static void printSquares( int n)
{
int square = 0, prev_x = 0;
for ( int x = 0; x < n; x++)
{
square = (square + x + prev_x);
Console.Write( square + " " );
prev_x = x;
}
}
static public void Main (){
int n = 5;
printSquares(n);
}
}
|
PHP
<?php
function printSquares( $n )
{
$square = 0; $prev_x = 0;
for ( $x = 0; $x < $n ; $x ++)
{
$square = ( $square + $x + $prev_x );
echo $square , " " ;
$prev_x = $x ;
}
}
$n = 5;
printSquares( $n );
?>
|
Javascript
<script>
function printSquares(n)
{
let square = 0, prev_x = 0;
for (let x = 0; x < n; x++)
{
square = (square + x + prev_x);
document.write(square + " " );
prev_x = x;
}
}
let n = 5;
printSquares(n);
</script>
|
Output:
0 1 4 9 16
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Sum of first n odd numbers are squares of natural numbers from 1 to n. For example 1, 1+3, 1+3+5, 1+3+5+7, 1+3+5+7+9, ….
Following is program based on above concept. Thanks to Aadithya Umashanker and raviteja for suggesting this method.
C++
#include<iostream>
using namespace std;
void printSquares( int n)
{
int square = 0, odd = 1;
for ( int x = 0; x < n; x++)
{
cout << square << " " ;
square = square + odd;
odd = odd + 2;
}
}
int main()
{
int n = 5;
printSquares(n);
}
|
Java
import java.io.*;
class GFG
{
static void printSquares( int n)
{
int square = 0 , odd = 1 ;
for ( int x = 0 ; x < n; x++)
{
System.out.print(square +
" " );
square = square + odd;
odd = odd + 2 ;
}
}
public static void main (String[] args)
{
int n = 5 ;
printSquares(n);
}
}
|
Python3
def printSquares(n):
square = 0
odd = 1
for x in range ( 0 , n):
print (square, end = " " )
square = square + odd
odd = odd + 2
n = 5 ;
printSquares(n)
|
C#
using System;
class GFG
{
static void printSquares( int n)
{
int square = 0, odd = 1;
for ( int x = 0; x < n; x++)
{
Console.Write(square + " " );
square = square + odd;
odd = odd + 2;
}
}
public static void Main ()
{
int n = 5;
printSquares(n);
}
}
|
PHP
<?php
function printSquares( $n )
{
$square = 0; $odd = 1;
for ( $x = 0; $x < $n ; $x ++)
{
echo $square , " " ;
$square = $square + $odd ;
$odd = $odd + 2;
}
}
$n = 5;
printSquares( $n );
?>
|
Javascript
<script>
function printSquares(n)
{
let square = 0, odd = 1;
for (let x = 0; x < n; x++)
{
document.write(square + " " );
square = square + odd;
odd = odd + 2;
}
}
let n = 5;
printSquares(n);
</script>
|
Output :
0 1 4 9 16
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Jul, 2022
Like Article
Save Article