Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:11:20+00:00 2026-06-13T11:11:20+00:00

So far my code runs fine, but I need a way to speed it

  • 0

So far my code runs fine, but I need a way to speed it up. When the user enters max_values to be 25000 it takes about 1.81 seconds and I need it to be less than one second. I tried my best to optimize my triples method but I don’t know what else to do.

import java.util.InputMismatchException;
import java.util.Scanner;

public class Pythagorean {

public static void triples(int max_values){
    int x = 0;
    for(int c = 5; c <= max_values; c++){
        int cTwo = c * c;
        int b = c - 1;
        for (int a = 0;a <= cTwo - b*b;a++){
            if (a*a + b*b == cTwo){
                x++;
                System.out.println(x + ") " + a + " " + b + " " +c);
            }
        }
    }
}

public static void main(String[] args){
    System.out.println("--- Pythagorean Triple Generator ---");
    System.out.println();
    Scanner input = new Scanner(System.in);
    int max_value = 0;
    System.out.print("Enter max value for c: ");
    try{
        max_value = input.nextInt();
    } catch (InputMismatchException ime) {
        input.nextLine();
        System.err.println("Error: Input is not an integer.");
        System.exit(1);
    }
    input.close();
    long start = System.currentTimeMillis();
    triples(max_value);
    double elapsed = (System.currentTimeMillis() - start)/ 1000.0;
    System.out.println("Searching complete...");
    System.out.printf("Elpased time: %.3f\n", elapsed);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:11:22+00:00Added an answer on June 13, 2026 at 11:11 am

    This just ran in 0.999 seconds on my PC.

    It uses a single StringBuilder to collect all the output, then does just one println at the end.

    public static void triples(final int max_values)
    {
        int x = 0;
        final StringBuilder sb = new StringBuilder(24000);
        for (int c = 5; c <= max_values; c++)
        {
            final int cTwo = c * c;
            final int b = c - 1;
            final int bTwo = b * b;
            final int cTwoLessB = cTwo - bTwo;
    
            for (int a = 0; a <= cTwoLessB; a++)
            {
                if (a * a + bTwo == cTwo)
                {
                    x++;
                    sb.append(x);
                    sb.append(") ");
                    sb.append(a);
                    sb.append(" ");
                    sb.append(b);
                    sb.append(" ");
                    sb.append(c);
                    sb.append("\n");
                }
            }
        }
        System.out.println(sb.toString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The application runs fine - But if the user selects NOTHING from the listboxes
I'm getting an wicked crash in native code. So far my code runs fine
I have this code so far which perfectly but relies on there being a
The code I am currently working on runs on Windows Server 2003, but needs
OS: Windows server 03 Python ver: 2.7 For the code below, its runs fine
This is what I have as far as code, it is in the first
How far is the code for best functionallity? I have two ComboBox, so the
This is my code thus far: ArrayList<String> matches = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String playString =
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
Hey im having this code so far - (id)initWithDictionary:(NSDictionary *)aDictionary { self = [super

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.