java


list all pythagorean triplets in java


I need to find all pythagorean triples (a^2+b^2=c^2) with values of A and B less than 100. My codes don't give me the correct output. Could anyone tell me what went wrong with my codes and write the correct codes instead?
public static void main(String[] args) {
int sum = 0;
for (int num1 = 1; num1 <= 100; num1++) {
for (int num2 = 1; num2 < 100; num2++) {
if (PerfectSquare(num1, num2, sum)) {
System.out.println(num1 + " " + num2);
}
}
}
}
public static boolean PerfectSquare(int number1, int number2, int sum) {
int square1;
int square2;
double root;
int sum1;
number1 = (int) ((99 - 1 + 1) * Math.random() + 1);
number2 = (int) ((99 - 1 + 1) * Math.random() + 1);
square1 = (int) Math.pow(number1, 2.0);
square2 = (int) Math.pow(number2, 2.0);
sum = square1 + square2;
root = Math.sqrt(sum);
sum1 = (int) Math.pow(root, 2.0);
if (!(sum == sum1)) {
return false;
}
return true;
}
You are overwriting your number1 and number2 parameters with random numbers:
number1 = (int) ((99 - 1 + 1 ) * Math.random() + 1);
number2 = (int) ((99 - 1 + 1 ) * Math.random() + 1);
You may as well not be passing in any values at all into the method if you do this. Remove these lines.
Also, you need to round your square root to an integer:
root = (int) Math.sqrt(sum);
since otherwise you will end up with the same number when you square it again.
Other points not related to correctness:
You can square integers without needing to cast by multiplying them with themselves:
square1 = number1 * number1;
This:
if (! (sum==sum1) ) {
is more easily written as
if (sum != sum1) {
Your sum method parameter is unnecessary. Declare it as a local variable.
You don't need to loop over all pairs of num1 and num2, since if (num1, num2) are the opposite and adjacent sides of a Pythagorean triple, then (num2, num1) clearly are too (similarly if num1 and num2 aren't, then num2 and num1 aren't either). As such, you can check fewer combinations using:
for (int num1 = 1; num1 <= 100 ; num1++) {
for (int num2 =1; num2 <num1; num2++) {

Related Links

How to find com.dji.GSDemo.GoogleMap in the project navigator and create DJIDemoApplication Class?
Externalize web.xml params in a Spring application
Using SOAP 1.2 binding instead of SOAP 1.1
After updating Intelij getting Exception in thread “main” java.lang.NoClassDefFoundError: org/testng/TestNG
Image Moments in Android Studio with Java
java.lang.IllegalStateException: container base add child?
How to send contsructor parameters from one class to another
Java Object Reference Passing Vs. C++ Memory Reference Passing
Google Drive service account domain-wide delegation not working offline
get time without time zone in Java
Java Package's location in online Java IDE?
Netbeans how to restore the compile errors popup [duplicate]
Custom LayoutManager
Spring JMS Concurrency and JMSXGroupID
Delete expired documents in CouchDB
Start date and end date from SQL procedure with JDBC

Categories

HOME
twitter
vbscript
zeromq
adfs
node-pdfkit
numeral.js
resize
jplayer
orchardcms
after-effects
invantive-sql
reactive-cocoa
pythonanywhere
grails3
ml
autoconf
intentfilter
cultureinfo
rundeck
emgucv
facet
nhapi
newline
gsoap
react-chartjs
data-manipulation
windows-dev-center
mozilla
language-concepts
google-api-nodejs-client
mpmediaquery
lto
gesture
serverside-rendering
libvpx
occlusion
keydown
sencha-touch-2.3
thin
websphere-mq-fte
multipeer-connectivity
rails-routing
clean-architecture
sage-one
pg-dump
flow-control
bind9
synchronous
google-feed-api
slicknav
savon
url-masking
measures
xml-attribute
mongocsharpdriver
autorest
fputcsv
jwplayer7
sigabrt
sdf
packagist
ios4
two-factor-authentication
kendonumerictextbox
collapse
simple-framework
tarjans-algorithm
twirl
fluid-dynamics
p2
sonarqube5.1.2
inmobi
csquery
camanjs
angularjs-ng-click
applescript-objc
uitouch
rebol3
cloud-connect
intentservice
chronoforms
orchardcms-1.7
smtp-auth
datawindow
pysimplesoap
jquery-ui-layout
self-extracting
pyinotify
sql-server-profiler
visitor-statistic
nsviewanimation
thunderbird-lightning
iweb
nerddinner
zune

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App