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;
}
Problem 3: Branches and Loops II (25pts). Consider the following circuit: Ri=521 = 12 V D WB+C V = 6 v L R =1003 ER-52 V - 24V E Ri= 150 1. Identify the branches and the loops of the circuit. 2. Using Kirschoff's rules and the loop current method, calculate the voltage and the current across each resistor. 3. Calculate the electric power dissipated in each resistor.
A transformer has 100 loops on the primary coil and 1000 loops on the secondary coil. If the primary coil is connected to 120 VAC, what will the voltage be between the leads in the secondary coil?
1 pts The primary coil of a transformer has 100 loops and is connected to a circuit with 120 volts (primary voltage). What is the output voltage (secondary voltage) if the secondary coil has 50 loops? 60 volts O 240 volts O 100 volts O 50 volts 1 pts Question 18 Question 18 1 pts If the current in the primary coil of the above problem is 2 amps, what is the output current (current in secondary coil)?SEP o 1...
C++ 1 Explain the properties and limitations of integer and floating-point numbers. 2 Explain nested branches and loops. 3 Explain the fundamental principles of testing and debugging 4 Design a class named EmployeeRecord that holds an employee
using python 1. Translate the following FOR loops to equivalent WHILE loops: a. for count in range(100): print(count) b. for count in range(1, 101): print(count) c. for count in range(100, 0, –1): print(count)
Suppose the branch frequencies are as follows: Conditional branches 15% Unconditional branches 1% Taken conditional branches 60% We are examining a 9-stage pipeline where the branch target address is calculated in the 3rd stage and the branch condition is evaluated in the 5th stage. Compare the performance of the machine when 90% of the conditional branches are predicted correctly with the performance of the machine when all the conditional branches are predicted to be untaken. Only branch stalls are considered
Write a java to find the Hailstone Sequence of a given number upto 1 using recursion. Example: The hailstone sequence starting at 13 is : 13 40 20 10 5 16 8 4 2 1 The length of the sequence is 10.
Question 2 Problem Definition Write a MATLAB script that uses nested loops to create a 100 by 100 element 2D array representing an image that shades from white at the top to black at the bottom. Your script should save the image in a file called test2.png. Testing The image file produced should be shaded from white at the top of the image to black at the bottom of the image like SO: Coding Write your code in a MATLAB...
Question 1 (5 marks): For the circuit shown in Figure 1 below, find the number of nodes, branches and loops, then use mesh analysis to find the currents and the power on dissipated in the 100, 10 and 120 resistors 1003 3120 ww 9A 412 Fig. 1
A 250V, 100 Hz sinusoidal supply is connected to a parallel network comprising three branches A, B, and C as follows: A, a coil of resistance 10 ohms; B, a series circuit of 5 ohms and inductive reactance of 9 ohms; C, a series circuit of 6 ohms, inductance of 50mH, and a capacitive reactance of 10 ohms. Determine a) The apparent power of the combination b) The rating and value of the capacitor to be connected in parallel to...