Question

armv8 sqaures 1 upto 100 loops and branches

armv8 sqaures 1 upto 100
loops and branches

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Starting from 1 to 100 check whether the current element is a perfect square or not. If yes then print it.

Below are the implementation of the above approach:

C++ implementation of the approach

#include <bits/stdc++.h>

using namespace std;

// Function to print all the perfect

// squares from the given range

void perfectSquares(float l, float r)

{

      // For every element from the range

    for (int i = l; i <= r; i++) {

          // If current element is

        // a perfect square

        if (sqrt(i) == (int)sqrt(i))

            cout << i << " ";

    }

}

  

// Driver code

int main()

{

    int l = 1, r = 100;

      perfectSquares(l, r);

      return 0;

}

Java implementation of the approach

import java.io.*;

class GFG

{

      

//Function to print all the perfect

// squares from the given range

static void perfectSquares(int l, int r)

{

     // For every element from the range

    for (int i = l; i <= r; i++)

    {

          // If current element is

        // a perfect square

        if (Math.sqrt(i) == (int)Math.sqrt(i))

            System.out.print(i + " ");

    }

}

  

// Driver code

public static void main (String[] args)

{

    int l = 1, r = 100;

    perfectSquares(l, r);

}

}

Python3 implementation of the approach

  

# Function to print all the perfect

# squares from the given range

def perfectSquares(l, r):

  

# For every element from the range

    for i in range(l, r + 1):

  

        # If current element is

        # a perfect square

        if (i**(.5) == int(i**(.5))):

            print(i, end=" ")

  

# Driver code

l = 1

r = 100

  

perfectSquares(l, r)

Output:

4 9 16 25 36 49 64 81 100

Efficient approach: This method is based on the fact that the very first perfect square after number L will definitely be the square of ⌈sqrt(L)⌉. In very simple terms, the square root of L will be very close to the number whose square root we are trying to find. Therefore, the number will be pow(ceil(sqrt(L)), 2).
The very first perfect square is important for this method. Now the original answer is hidden over this pattern i.e. 0 1 4 9 16 25
the difference between 0 and 1 is 1
the difference between 1 and 4 is 3
the difference between 4 and 9 is 5 and so on…
which means that the difference between two perfect squares is always an odd number.

Now, the question arises what must be added to get the next number and the answer is (sqrt(X) * 2) + 1 where X is the already known perfect square.

Let the current perfect square be 4 then the next perfect square will definitely be 4 + (sqrt(4) * 2 + 1) = 9. Here, number 5 is added and the next number to be added will be 7 then 9 and so on… which makes a series of odd numbers.

Addition is computationally less expensive than performing multiplication or finding square roots of every number.

C++ implementation of the approach

#include <bits/stdc++.h>

using namespace std;

  

// Function to print all the perfect

// squares from the given range

void perfectSquares(float l, float r)

{

  

    // Getting the very first number

    int number = ceil(sqrt(l));

  

    // First number's square

    int n2 = number * number;

  

    // Next number is at the difference of

    number = (number * 2) + 1;

  

    // While the perfect squares

    // are from the range

    while ((n2 >= l && n2 <= r)) {

  

        // Print the perfect square

        cout << n2 << " ";

  

        // Get the next perfect square

        n2 = n2 + number;

  

        // Next odd number to be added

        number += 2;

    }

}

  

// Driver code

int main()

{

    int l = 1, r = 100;

  

    perfectSquares(l, r);

  

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
armv8 sqaures 1 upto 100 loops and branches
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT