Skip to main content
edited body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5.

Currently, iI am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5.

Currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5.

Currently, I am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}
added 93 characters in body
Source Link
JanB
  • 83
  • 1
  • 5

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5.

currentlyCurrently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5

currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5.

Currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}
Became Hot Network Question
added 1 character in body
Source Link
JanB
  • 83
  • 1
  • 5

I am writing a program that allows me to find all possible pairs of square numbers. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5

currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {255,25,3,25,4,2,525};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5

currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {25,25,3,25,4,2,5};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}

I am writing a program that allows me to find all possible pairs of square numbers. e.g an array of {5,25,3,25,4,2,25} will return [5,25],[5,25],[2,4],[5,25] since 25 is square of 5

currently, i am using a nested for loop to find the squares. I'm just wondering if there is a better way to do this?

import java.lang.Math.*;

public static void main(String args[])
{
    int arr[] = {5,25,3,25,4,2,25};
    String s = "";
    
    for(int i =0; i < arr.length;i++)
    {

        for(int j = 0;j < arr.length;j++)
        {
            if(Math.sqrt(arr[i]) == arr[j])
            {
                s += arr[j] + "," + arr[i] + " ";
            }
        }
        
    }
    
    System.out.println(s);

}
added 1 character in body
Source Link
JanB
  • 83
  • 1
  • 5
Loading
edited tags
Link
JanB
  • 83
  • 1
  • 5
Loading
Source Link
JanB
  • 83
  • 1
  • 5
Loading