Print squares of first n natural numbers without using *, / and –
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);
}
}
|
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>
|
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 );
?>
|
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);
}
}
|
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>
|
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 );
?>
|
Output :
0 1 4 9 16
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...