Нахождение всех возможных пифагорейских треугольников в наборе треугольников

ПЕРЕСМОТР 1

Я посмотрел на эту тему: Все возможные пифагорейские тройки

Похож на мой, но я не могу заставить мою работать по какой-то причине ..
Вот мой код:

#include <iostream>
#include <cmath>
using namespace std;

class Labb2_uppg1 {
public:
void run() {
compute();
}
private:
void compute() {                                                            // performs the computations
int a, b, c, counter = 0, counter_pyth = 0;                             // a = cathetus of triangle
// b = cathetus of triangle
// c = hypotenuse of triangle
// counter = used to determine the amount of iterated triangles
// counter_pyth = used to determine the subset of pythagorean triangles in the total amount of iterated triangles
for (a = 1; a <= 500; a++) {
for (b = a; b <= 500; b++) {                                        // starts from the same index as 'a' to omit duplicate results
counter++;
c = int_sqrt((a*a)+(b*b));
if (c == 100 || c == 200 || c == 300 || c == 400 || c == 500)   // prints the triangles whose hypotenuse matches the integers 100, 200, 300, 400 and 500
cout << "Triangel med hypotenusan " << c << ": a = " << a << ", b = " << b << endl;
if (((a*a)+(b*b))==(c*c)) {
cout << "DEBUG_01: a = " << a << ", b = " << b
<< ", c = " << c << endl;
counter_pyth++;
}
}
}
cout << "\nAntal itererade trianglar: " << counter;                     // prints the number of triangles that the program has iterated through
cout << "\nAntal pytagoreiska trianglar: " << counter_pyth << "\n\n";   // prints the number of Pythagorean triangles that the program has iterated through
}

int int_sqrt(int r) {                                                       // simplification of the Bakhsali Approximation method to calculate square roots
// only integers may be used when invoking this function
// returns the integer square root if it was successfully calculated using the radicand r
int i = 0;                                                              // initiates 'i' to a value of 0
while ((i*i) <= r)                                                      // approximates the square of 'i' to the radicand 'r'
i++;
i--;

if ((i*i) == r)                                                         // if the square of 'i' is exactly 'r'
return i;                                                           // then the integer root of 'r' has successfully been found
else
return -1;                                                          // else -1 is returned, no real number (radicand) can have -1 as its root
}

};

Выход:

DEBUG_01: а = 95, б = 228, с = 247

DEBUG_01: а = 96, б = 110, с = 146

DEBUG_01: a = 96, b = 128, c = 160

DEBUG_01: а = 96, б = 180, с = 204

DEBUG_01: a = 96, b = 247, c = 265

DEBUG_01: а = 96, б = 280, с = 296

DEBUG_01: a = 96, b = 378, c = 390

DEBUG_01: а = 98, б = 336, с = 350

DEBUG_01: а = 99, б = 132, с = 165

DEBUG_01: а = 99, б = 168, с = 195

DEBUG_01: а = 99, б = 440, с = 451

DEBUG_01: а = 100, б = 105, с = 145

DEBUG_01: а = 100, б = 240, с = 260

DEBUG_01: а = 100, б = 495, с = 505

DEBUG_01: а = 102, б = 136, с = 170

DEBUG_01: а = 102, б = 280, с = 298

DEBUG_01: а = 104, б = 153, с = 185

DEBUG_01: а = 104, б = 195, с = 221

DEBUG_01: а = 104, б = 330, с = 346

DEBUG_01: a = 105, b = 140, c = 175

DEBUG_01: а = 105, б = 208, с = 233

DEBUG_01: a = 105, b = 252, c = 273

DEBUG_01: a = 105, b = 360, c = 375

DEBUG_01: а = 108, б = 144, с = 180

DEBUG_01: а = 108, б = 231, с = 255

DEBUG_01: а = 108, b = 315, с = 333

DEBUG_01: а = 108, б = 480, с = 492

DEBUG_01: а = 110, б = 264, с = 286

DEBUG_01: а = 111, б = 148, с = 185

DEBUG_01: a = 112, b = 180, c = 212

DEBUG_01: а = 112, б = 210, с = 238

Триангель мед гипотенузан 400: а = 112, б = 384

DEBUG_01: a = 112, b = 384, c = 400

DEBUG_01: a = 112, b = 441, c = 455

DEBUG_01: a = 114, b = 152, c = 190

DEBUG_01: a = 114, b = 352, c = 370

DEBUG_01: a = 115, b = 252, c = 277

DEBUG_01: a = 115, b = 276, c = 299

DEBUG_01: а = 117, б = 156, с = 195

DEBUG_01: а = 117, б = 240, с = 267

DEBUG_01: а = 119, б = 120, с = 169

DEBUG_01: a = 119, b = 408, c = 425

DEBUG_01: а = 120, б = 126, с = 174

Триангель мед гипотенузан 200: а = 120, б = 160

DEBUG_01: а = 120, б = 160, с = 200

DEBUG_01: а = 120, б = 182, с = 218

DEBUG_01: а = 120, б = 209, с = 241

DEBUG_01: а = 120, б = 225, с = 255

DEBUG_01: а = 120, б = 288, с = 312

DEBUG_01: а = 120, б = 350, с = 370

DEBUG_01: а = 120, б = 391, с = 409

DEBUG_01: а = 120, б = 442, с = 458

DEBUG_01: а = 123, б = 164, с = 205

DEBUG_01: а = 125, б = 300, с = 325

DEBUG_01: а = 126, б = 168, с = 210

DEBUG_01: a = 126, b = 432, c = 450

DEBUG_01: а = 128, б = 240, с = 272

DEBUG_01: а = 129, б = 172, с = 215

DEBUG_01: а = 130, б = 144, с = 194

DEBUG_01: а = 130, б = 312, с = 338

DEBUG_01: а = 132, б = 176, с = 220

DEBUG_01: а = 132, б = 224, с = 260

DEBUG_01: а = 132, б = 351, с = 375

DEBUG_01: а = 132, б = 385, с = 407

DEBUG_01: а = 132, б = 475, с = 493

DEBUG_01: а = 133, б = 156, с = 205

DEBUG_01: а = 133, б = 456, с = 475

DEBUG_01: а = 135, б = 180, с = 225

DEBUG_01: a = 135, b = 324, c = 351

DEBUG_01: а = 135, б = 352, с = 377

DEBUG_01: а = 136, б = 255, с = 289

DEBUG_01: а = 136, б = 273, с = 305

DEBUG_01: а = 138, б = 184, с = 230

DEBUG_01: а = 140, б = 147, с = 203

DEBUG_01: а = 140, б = 171, с = 221

DEBUG_01: а = 140, б = 225, с = 265

DEBUG_01: а = 140, б = 336, с = 364

Триангель мед гипотенузан 500: а = 140, б = 480

DEBUG_01: а = 140, б = 480, с = 500

DEBUG_01: а = 141, б = 188, с = 235

DEBUG_01: а = 144, б = 165, с = 219

DEBUG_01: а = 144, б = 192, с = 240

DEBUG_01: а = 144, б = 270, с = 306

DEBUG_01: а = 144, б = 308, с = 340

DEBUG_01: а = 144, б = 420, с = 444

DEBUG_01: а = 145, б = 348, с = 377

DEBUG_01: а = 145, б = 408, с = 433

DEBUG_01: а = 147, б = 196, с = 245

DEBUG_01: а = 150, б = 200, с = 250

DEBUG_01: а = 150, б = 360, с = 390

DEBUG_01: а = 152, б = 285, с = 323

DEBUG_01: а = 152, б = 345, с = 377

DEBUG_01: а = 153, б = 204, с = 255

DEBUG_01: а = 153, б = 420, с = 447

DEBUG_01: а = 155, б = 372, с = 403

DEBUG_01: а = 155, б = 468, с = 493

DEBUG_01: а = 156, б = 208, с = 260

DEBUG_01: а = 156, б = 320, с = 356

DEBUG_01: а = 156, б = 455, с = 481

DEBUG_01: а = 156, б = 495, с = 519

DEBUG_01: а = 159, б = 212, с = 265

DEBUG_01: а = 160, б = 168, с = 232

DEBUG_01: а = 160, б = 231, с = 281

DEBUG_01: а = 160, б = 300, с = 340

DEBUG_01: а = 160, б = 384, с = 416

DEBUG_01: а = 161, б = 240, с = 289

DEBUG_01: а = 162, б = 216, с = 270

DEBUG_01: а = 165, б = 220, с = 275

DEBUG_01: а = 165, б = 280, с = 325

DEBUG_01: а = 165, б = 396, с = 429

DEBUG_01: а = 168, б = 224, с = 280

DEBUG_01: а = 168, б = 270, с = 318

DEBUG_01: а = 168, б = 315, с = 357

DEBUG_01: a = 168, b = 374, c = 410

DEBUG_01: а = 168, б = 425, с = 457

DEBUG_01: а = 168, б = 490, с = 518

DEBUG_01: а = 170, б = 264, с = 314

DEBUG_01: а = 170, б = 408, с = 442

DEBUG_01: a = 171, b = 228, c = 285

DEBUG_01: а = 174, б = 232, с = 290

DEBUG_01: a = 175, b = 288, c = 337

DEBUG_01: a = 175, b = 420, c = 455

DEBUG_01: а = 176, б = 210, с = 274

DEBUG_01: а = 176, б = 330, с = 374

Триангель мед гипотенузан 500: а = 176, б = 468

DEBUG_01: a = 176, b = 468, c = 500

DEBUG_01: а = 177, б = 236, с = 295

DEBUG_01: а = 180, б = 189, с = 261

Триангель мед гипотенузан 300: а = 180, б = 240

DEBUG_01: а = 180, б = 240, с = 300

DEBUG_01: а = 180, б = 273, с = 327

DEBUG_01: а = 180, б = 299, с = 349

DEBUG_01: a = 180, b = 385, c = 425

DEBUG_01: а = 180, б = 432, с = 468

DEBUG_01: a = 183, b = 244, c = 305

DEBUG_01: a = 184, b = 345, c = 391

DEBUG_01: a = 185, b = 444, c = 481

DEBUG_01: а = 186, б = 248, с = 310

DEBUG_01: а = 189, б = 252, с = 315

DEBUG_01: а = 189, б = 340, с = 389

DEBUG_01: а = 190, б = 336, с = 386

DEBUG_01: а = 190, б = 456, с = 494

DEBUG_01: а = 192, б = 220, с = 292

DEBUG_01: а = 192, б = 256, с = 320

DEBUG_01: а = 192, б = 360, с = 408

DEBUG_01: а = 192, б = 494, с = 530

DEBUG_01: а = 195, б = 216, с = 291

DEBUG_01: а = 195, б = 260, с = 325

DEBUG_01: а = 195, б = 400, с = 445

DEBUG_01: а = 195, б = 468, с = 507

DEBUG_01: а = 196, б = 315, с = 371

DEBUG_01: a = 198, b = 264, c = 330

DEBUG_01: а = 198, б = 336, с = 390

DEBUG_01: а = 200, б = 210, с = 290

DEBUG_01: a = 200, b = 375, c = 425

DEBUG_01: а = 200, б = 480, с = 520

DEBUG_01: а = 201, б = 268, с = 335

DEBUG_01: а = 203, б = 396, с = 445

DEBUG_01: а = 204, б = 253, с = 325

DEBUG_01: а = 204, б = 272, с = 340

DEBUG_01: а = 205, б = 492, с = 533

DEBUG_01: а = 207, б = 224, с = 305

DEBUG_01: а = 207, б = 276, с = 345

DEBUG_01: а = 208, б = 306, с = 370

DEBUG_01: a = 208, b = 390, c = 442

DEBUG_01: а = 210, б = 280, с = 350

DEBUG_01: а = 210, б = 416, с = 466

DEBUG_01: a = 213, b = 284, c = 355

DEBUG_01: a = 216, b = 288, c = 360

DEBUG_01: а = 216, б = 405, с = 459

DEBUG_01: a = 216, b = 462, c = 510

DEBUG_01: a = 217, b = 456, c = 505

DEBUG_01: a = 219, b = 292, c = 365

DEBUG_01: а = 220, б = 231, с = 319

DEBUG_01: а = 220, б = 459, с = 509

DEBUG_01: а = 222, б = 296, с = 370

DEBUG_01: а = 224, б = 360, с = 424

DEBUG_01: а = 224, б = 420, с = 476

DEBUG_01: а = 225, б = 272, с = 353

DEBUG_01: а = 225, б = 300, с = 375

DEBUG_01: а = 228, б = 304, с = 380

DEBUG_01: а = 228, б = 325, с = 397

DEBUG_01: а = 231, б = 308, с = 385

DEBUG_01: а = 231, b = 392, с = 455

DEBUG_01: а = 232, б = 435, с = 493

DEBUG_01: а = 234, б = 312, с = 390

DEBUG_01: а = 234, б = 480, с = 534

DEBUG_01: а = 237, б = 316, с = 395

DEBUG_01: а = 238, б = 240, с = 338

DEBUG_01: а = 240, б = 252, с = 348

DEBUG_01: а = 240, б = 275, с = 365

Триангель мед гипотенузан 400: а = 240, б = 320

DEBUG_01: а = 240, б = 320, с = 400

DEBUG_01: а = 240, б = 364, с = 436

DEBUG_01: а = 240, б = 418, с = 482

DEBUG_01: а = 240, б = 450, с = 510

DEBUG_01: а = 243, б = 324, с = 405

DEBUG_01: а = 246, б = 328, с = 410

DEBUG_01: а = 248, б = 465, с = 527

DEBUG_01: а = 249, б = 332, с = 415

DEBUG_01: а = 252, б = 275, с = 373

DEBUG_01: а = 252, б = 336, с = 420

DEBUG_01: а = 252, б = 405, с = 477

DEBUG_01: а = 255, б = 340, с = 425

DEBUG_01: а = 255, б = 396, с = 471

DEBUG_01: а = 256, б = 480, с = 544

DEBUG_01: a = 258, b = 344, c = 430

DEBUG_01: а = 260, б = 273, с = 377

DEBUG_01: а = 260, б = 288, с = 388

DEBUG_01: а = 261, b = 348, с = 435

DEBUG_01: a = 261, b = 380, c = 461

DEBUG_01: a = 264, b = 315, c = 411

DEBUG_01: a = 264, b = 352, c = 440

DEBUG_01: a = 264, b = 448, c = 520

DEBUG_01: а = 264, b = 495, с = 561

DEBUG_01: a = 266, b = 312, c = 410

DEBUG_01: a = 267, b = 356, c = 445

DEBUG_01: a = 270, b = 360, c = 450

DEBUG_01: a = 273, b = 364, c = 455

DEBUG_01: a = 276, b = 368, c = 460

DEBUG_01: a = 276, b = 493, c = 565

DEBUG_01: а = 279, b = 372, с = 465

DEBUG_01: a = 279, b = 440, c = 521

DEBUG_01: а = 280, б = 294, с = 406

DEBUG_01: а = 280, б = 342, с = 442

DEBUG_01: a = 280, b = 351, c = 449

DEBUG_01: a = 280, b = 450, c = 530

DEBUG_01: a = 282, b = 376, c = 470

DEBUG_01: a = 285, b = 380, c = 475

DEBUG_01: а = 288, б = 330, с = 438

DEBUG_01: a = 288, b = 384, c = 480

DEBUG_01: a = 291, b = 388, c = 485

DEBUG_01: а = 294, б = 392, с = 490

DEBUG_01: а = 297, б = 304, с = 425

DEBUG_01: а = 297, б = 396, с = 495

DEBUG_01: а = 300, б = 315, с = 435

Триангель мед гипотенузан 500: а = 300, б = 400

DEBUG_01: а = 300, б = 400, с = 500

DEBUG_01: а = 300, б = 455, с = 545

DEBUG_01: а = 303, б = 404, с = 505

DEBUG_01: а = 306, б = 408, с = 510

DEBUG_01: а = 308, б = 435, с = 533

DEBUG_01: а = 308, б = 495, с = 583

DEBUG_01: а = 309, б = 412, с = 515

DEBUG_01: а = 312, б = 416, с = 520

DEBUG_01: a = 312, b = 459, c = 555

DEBUG_01: а = 315, б = 420, с = 525

DEBUG_01: a = 318, b = 424, c = 530

DEBUG_01: а = 319, б = 360, с = 481

DEBUG_01: а = 320, б = 336, с = 464

DEBUG_01: а = 320, б = 462, с = 562

DEBUG_01: a = 321, b = 428, c = 535

DEBUG_01: а = 322, б = 480, с = 578

DEBUG_01: a = 324, b = 432, c = 540

DEBUG_01: а = 325, б = 360, с = 485

DEBUG_01: а = 327, б = 436, с = 545

DEBUG_01: а = 330, б = 440, с = 550

DEBUG_01: а = 333, б = 444, с = 555

DEBUG_01: а = 336, б = 377, с = 505

DEBUG_01: а = 336, б = 385, с = 511

DEBUG_01: а = 336, б = 448, с = 560

DEBUG_01: а = 339, б = 452, с = 565

DEBUG_01: а = 340, б = 357, с = 493

DEBUG_01: а = 341, б = 420, с = 541

DEBUG_01: а = 342, б = 456, с = 570

DEBUG_01: а = 345, б = 460, с = 575

DEBUG_01: а = 348, б = 464, с = 580

DEBUG_01: а = 351, б = 468, с = 585

DEBUG_01: а = 352, б = 420, с = 548

DEBUG_01: а = 354, б = 472, с = 590

DEBUG_01: а = 357, б = 360, с = 507

DEBUG_01: а = 357, б = 476, с = 595

DEBUG_01: а = 360, б = 378, с = 522

DEBUG_01: а = 360, б = 480, с = 600

DEBUG_01: a = 363, b = 484, c = 605

DEBUG_01: a = 366, b = 488, c = 610

DEBUG_01: a = 368, b = 465, c = 593

DEBUG_01: a = 369, b = 492, c = 615

DEBUG_01: a = 372, b = 496, c = 620

DEBUG_01: a = 375, b = 500, c = 625

DEBUG_01: а = 380, б = 399, с = 551

DEBUG_01: a = 384, b = 440, c = 584

DEBUG_01: a = 390, b = 432, c = 582

DEBUG_01: a = 396, b = 403, c = 565

DEBUG_01: а = 399, б = 468, с = 615

DEBUG_01: а = 400, б = 420, с = 580

DEBUG_01: a = 414, b = 448, c = 610

DEBUG_01: a = 420, b = 441, c = 609

DEBUG_01: a = 429, b = 460, c = 629

DEBUG_01: а = 432, б = 495, с = 657

DEBUG_01: а = 440, б = 462, с = 638

DEBUG_01: a = 460, b = 483, c = 667

DEBUG_01: a = 476, b = 480, c = 676

Треугольник Antal Itererade: 125250
Антал, пятагорейска, треугольник: 456


Как вы можете видеть, всякий раз, когда я запускаю это, я получаю ответ «456» от «counter_pyth», по словам моего учителя, правильный ответ «386», но я не знаю, что я сделал неправильно …


ПЕРЕСМОТР 2

Код:

.
.
.

for (a = 1; a <= 500; a++) {
for (b = a; b <= 500; b++) {                                        // starts from the same index as 'a' to omit duplicate results
counter++;
c = int_sqrt((a*a)+(b*b));
if (c == 100 || c == 200 || c == 300 || c == 400 || c == 500)   // prints the triangles whose hypotenuse matches the integers 100, 200, 300, 400 and 500
cout << "Triangel med hypotenusan " << c << ": a = " << a << ", b = " << b << endl;
if (((a*a)+(b*b))==(c*c)) {
if (!((a > 500 ) || (b > 500) || (c > 500))) {
cout << "DEBUG_01: a = " << a << ", b = " << b
<< ", c = " << c << endl;
counter_pyth++;
}
}
}
}

0

Решение

(Продолжая с вопросом, который вы связали) Попробуйте подсчитать с c вместо этого, так как c должно быть не более 500 и нет ограничений на a или же b:

for(int c = 1; c <= 500; c++) {
for(int a = 1; a < c; a++) {
int b = int_sqrt(c * c - a * a);
if(b > 0) {
//you've found a right triangle with c at most 500
}
}
}

Вы, вероятно, считаете более 386 треугольников, потому что вы считаете с c > 500,

1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]