Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I've exceeded the time limit on Sherlock and Squares on hackerrank.com.

Problem

###Problem### GivenGiven the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

Sample Input

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

Sample Output

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

My code in C++

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange.

I've exceeded the time limit on Sherlock and Squares on hackerrank.com.

###Problem### Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange.

I've exceeded the time limit on Sherlock and Squares on hackerrank.com.

Problem

Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

Sample Input

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

Sample Output

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

My code in C++

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange.

Tweeted twitter.com/StackCodeReview/status/749466387891052544
deleted 11 characters in body; edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

HackerRank Sherlock and Squares: "Terminated due to timeout"count perfect squares in an interval

I've exceeded the time limit on Sherlock and Squares on hackerrank.com.

###Problem### Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange. Thank you!

HackerRank Sherlock and Squares: "Terminated due to timeout"

###Problem### Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange. Thank you!

HackerRank Sherlock and Squares: count perfect squares in an interval

I've exceeded the time limit on Sherlock and Squares on hackerrank.com.

###Problem### Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange.

removed images, provided a description of the problem to solve
Source Link

I am unsure###Problem### Given the number of how I can simplify my code for this HackerRank "Sherlocktest cases and Squares" activitya range (inclusive), find the number of numbers within that range that are perfect squares.

Here's my###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++:C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange. Thank you!

I am unsure of how I can simplify my code for this HackerRank "Sherlock and Squares" activity.

Here's my code in C++:

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange. Thank you!

###Problem### Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect squares.

###Sample Input###

  • 2 (number of test cases)
  • 3 9 (first test case)
  • 17 24 (second test case)

###Sample Output###

  • 2 (in range [3,9], there are two perfect square numbers)
  • 0 (in range [17, 24], there are no perfect square numbers)

##My code in C++##

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
 unsigned test_cases, low_range, high_range, count = 0;
 vector<unsigned> sherlock_squares;
 cin >> test_cases;
 while(test_cases != 0)
 {
 cin >> low_range >> high_range;
 for(unsigned i = low_range; i <= high_range; ++i)
 {
 if(sqrt(i) == floor(sqrt(i)))
 count += 1;
 }
 sherlock_squares.push_back(count);
 count = 0;
 --test_cases;
 }
 for(unsigned i = 0; i < sherlock_squares.size() - 1; ++i)
 cout << sherlock_squares.at(i) << endl;
 cout << sherlock_squares.at(sherlock_squares.size() - 1);
 return 0;
}

I'd appreciate it if an explanation is provided in your response, since I'm new to programming and Stack Exchange. Thank you!

removed images
Source Link
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /