Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmPrint n numbers such that their sum is a perfect square

Print n numbers such that their sum is a perfect square

Given an integer n, the task is to print n numbers such that their sum is a perfect square.
Examples: 

Input: n = 3 
Output: 1 3 5 
1 + 3 + 5 = 9 = 32

Input: n = 4 
Output: 1 3 5 7 
1 + 3 + 5 + 7 = 16 = 42 

Approach: The sum of first n odd numbers is always a perfect square. So, we will print the first n odd numbers as the output.
Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print n numbers such that
// their sum is a perfect square
void findNumbers(int n)
{
    int i = 1;
    while (i <= n) {
 
        // Print ith odd number
        cout << ((2 * i) - 1) << " ";
        i++;
    }
}
 
// Driver code
int main()
{
    int n = 3;
    findNumbers(n);
}


Java




// Java implementation of the approach
import java.util.*;
import java.io.*;
class GFG {
 
    // Function to print n numbers such that
    // their sum is a perfect square
    static void findNumbers(int n)
    {
        int i = 1;
        while (i <= n) {
 
            // Print ith odd number
            System.out.print(((2 * i) - 1) + " ");
            i++;
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 3;
        findNumbers(n);
    }
}


Python3




# Python3 implementation of the approach
 
# Function to print n numbers such that
# their sum is a perfect square
def findNumber(n):
    i = 1
    while i <= n:
 
        # Print ith odd number
        print((2 * i) - 1, end = " ")
        i += 1
 
# Driver code    
n = 3
findNumber(n)
 
# This code is contributed by Shrikant13


C#




// C# implementation of the approach
using System;
public class GFG {
 
    // Function to print n numbers such that
    // their sum is a perfect square
    public static void findNumbers(int n)
    {
        int i = 1;
        while (i <= n) {
 
            // Print ith odd number
            Console.Write(((2 * i) - 1) + " ");
            i++;
        }
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n = 3;
        findNumbers(n);
    }
}
 
// This code is contributed by Shrikant13


PHP




<?php
// PHP implementation of the approach
 
// Function to print n numbers such that
// their sum is a perfect square
function findNumbers($n)
{
    $i = 1;
    while ($i <= $n)
    {
 
        // Print ith odd number
        echo ((2 * $i) - 1) . " ";
        $i++;
    }
}
 
// Driver code
$n = 3;
findNumbers($n);
 
// This code contributed by PrinciRaj1992
?>


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to print n numbers such that
// their sum is a perfect square
function findNumbers(n)
{
    var i = 1;
    while (i <= n) {
 
        // Print ith odd number
        document.write(((2 * i) - 1)+" ") ;
        i++;
    }
}
 
var n = 3;
    findNumbers(n);
 
</script>


Output

1 3 5 

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time.
Auxiliary Space: O(1), as we are not using any extra space.

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 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments