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

Can't unset fullscreen mode in JavaFX
MySql not working on Amazon EC2
How to gracefully stop the executorservice with a stopfile approach
Why does this Calendar fail?
How to change a int number to the asci equivalent?
Circumventing ImageIO.write's slowness
Adding MouseListeners for JToolBar (events are consumed?)
“while” loop questions for Java
How can I get the (video) activities of a user with the YouTube API v3 in Java
Change endpoint reference on Remote object. Is it possible?
Unable to use method accessors on attributes in a session using JSTL
Java View Html Code as web-page
Slider hides sub-menus
Changing the Size of action Bar
redirecting to youtube to upload video available on jsp
Regular Expression breaking Due to line break character (\n)

Categories

HOME
ms-access
vim
openlayers
bluetooth
oracle11g
drivers
google-api-php-client
relative-path
raspberry-pi
serverspec
echarts
azure-media-services
slick-slider
gitpitch
primary-key
communication
imacros
quickbooks
windows-azure-storage
iron-router
vaadin7
worldwind
lldb
wkwebview
mustache.php
realex-payments-api
hammerspoon
trading
javacv
centos6.5
kannel
greendao
maquette
buck
file-rename
sql-server-2012-express
bluestacks
uisplitview
gtrendsr
commit
windows-dev-center
uiswipegesturerecognizer
stacked
git-merge
swift3.0.2
bootstrapper
ensembles
core-plot
powershell-dsc
jvm-languages
vao
babel-core
menuitem
flickr-api
jlink
carrot
flutterwave
gcsfuse
quartz-composer
url-masking
dstu2-fhir
py2app
impresspages
connect-by
goose
jms2
icu4j
feedback
fouc
embedded-code
microbenchmark
sysinternals
myo
bigbluebutton
rake-task
dd
jscript.net
knuth
android-nested-fragment
preferences
eol
phpthumb
datagridviewcolumn
hyprlinkr
ember-charts
message-driven-bean
stxxl
apc
odata4j
flash-builder4.5
excel-2003
shellexecute
dataadapter
jquery-mobile-dialog
sabredav
windows-phone-7.1.1
osql
whoosh
deobfuscation
gcj
self-extracting
winbugs14
disclosure
datareader
coda-slider
dbal
mirah
swing-app-framework
mediarss
.net-1.0
commodore
signal-handling

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