Note that there are some explanatory texts on larger screens.

plurals
  1. POalgorithm for finding primitive pythagorean triples
    text
    copied!<p>here is my code:</p> <pre class="lang-c prettyprint-override"><code>// PPT.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define LOWER_BOUND 1 #define UPPER_BOUND 20 struct ppt { int v1; int v2; int v3; ppt *next; }; typedef struct ppt PPT; typedef PPT *ppt_ptr; void insert_ppt(ppt_ptr *h_ptr, ppt_ptr *t_ptr, int u1, int u2, int u3); void print_ppt(ppt_ptr curr_ptr); int is_prime(int n); int is_pythagorean_triplet(int v1, int v2, int v3); int different_triples(int v1, int v2, int v3, int u1, int u2, int u3); int are_exact_multiples(int p, int q, int r, int l, int m, int n); int is_unique_and_insertable(ppt_ptr curr_ptr, int v1, int v2, int v3); //==================================================================== int _tmain(int argc, _TCHAR* argv[]) { ppt_ptr head_ptr = NULL; ppt_ptr tail_ptr = NULL; for (int a = LOWER_BOUND; a &lt;= UPPER_BOUND; a++) { for (int b = LOWER_BOUND; b &lt;= UPPER_BOUND; b++) { for (int c = LOWER_BOUND; c &lt;= UPPER_BOUND; c++) { if(is_pythagorean_triplet(a,b,c) == 1) { if(head_ptr == NULL) { //printf("%d %d %d",a,b,c); insert_ppt(&amp;head_ptr,&amp;tail_ptr,a,b,c); } //printf("%d %d %d",a,b,c); if(is_unique_and_insertable(tail_ptr,a,b,c) == 1) { //printf("%d %d %d\n",a,b,c); insert_ppt(&amp;head_ptr,&amp;tail_ptr,a,b,c); } } } } } //print_ppt(head_ptr); getchar(); getchar(); return 0; } </code></pre> <p>this function inserts a new node at end of list</p> <pre class="lang-c prettyprint-override"><code>void insert_ppt(ppt_ptr *h_ptr, ppt_ptr *t_ptr, int u1, int u2, int u3) { ppt_ptr new_ptr; new_ptr = ppt_ptr( malloc( sizeof(PPT) ) ); if(new_ptr != NULL) { new_ptr-&gt;v1 = u1; new_ptr-&gt;v2 = u2; new_ptr-&gt;v3 = u3; new_ptr-&gt;next = NULL; if(*h_ptr == NULL) { *h_ptr = new_ptr; } else { (*t_ptr)-&gt;next = new_ptr; } *t_ptr = new_ptr; } else { printf("%d %d %d not inserted. No memory available.\n",u1,u2,u3); } } </code></pre> <p>this function prints list </p> <pre class="lang-c prettyprint-override"><code>void print_ppt(ppt_ptr curr_ptr) { if(curr_ptr == NULL) { printf("List is empty.\n\n"); } else { while(curr_ptr != NULL) { printf("%d %d %d\n",curr_ptr-&gt;v1,curr_ptr-&gt;v2,curr_ptr-&gt;v3); curr_ptr = curr_ptr-&gt;next; } } } </code></pre> <p>this function determines if a number is prime </p> <pre class="lang-c prettyprint-override"><code>// Function 1 int is_prime(int n) { int num_of_factors = 0; int i = 1; for (i=1; i&lt;=n; i++) { if (n % i == 0) { num_of_factors ++; } } if (num_of_factors == 2) { return 1; } else { return 0; } } </code></pre> <p>this function determines if a triplet is pythagorean </p> <pre class="lang-c prettyprint-override"><code>// Function 2 int is_pythagorean_triplet(int v1, int v2, int v3) { if ( (v1*v1 + v2*v2 == v3*v3) || (v2*v2 + v3*v3 == v1*v1) || (v1*v1 + v3*v3 == v2*v2) ) { return 1; } else { return 0; } } </code></pre> <p>this function determines if a triplet is unique and this is the function that i am having trouble with </p> <pre class="lang-c prettyprint-override"><code>int is_unique_and_insertable(ppt_ptr curr_ptr, int v1, int v2, int v3) { if(curr_ptr == NULL) { //printf("List is empty.\n\n"); } else { if(curr_ptr != NULL) { //printf("%d %d %d\n",curr_ptr-&gt;v1,curr_ptr-&gt;v2,curr_ptr-&gt;v3); int u1 = curr_ptr-&gt;v1; int u2 = curr_ptr-&gt;v2; int u3 = curr_ptr-&gt;v3; if( (different_triples(v1,v2,v3,u1,u2,u3)) &amp;&amp; (!are_exact_multiples(v1,v2,v3,u1,u2,u3) ) ) { printf("%d %d %d\n",curr_ptr-&gt;v1,curr_ptr-&gt;v2,curr_ptr-&gt;v3); return 1; } //printf("yoyoyo"); curr_ptr = curr_ptr-&gt;next; } } return 0; } </code></pre> <p>this function determines if a triple is unique </p> <pre class="lang-c prettyprint-override"><code>// Definition: This function checks if &lt;v1,v2,v3&gt; and &lt;u1,u2,u3&gt; are different triplets // or not. If they are different triplets, it returns 1. int different_triples(int v1, int v2, int v3, int u1, int u2, int u3) { if (v1==u1 &amp;&amp; v2==u2 &amp;&amp; v3==u3) return 0; else if (v1==u1 &amp;&amp; v2==u3 &amp;&amp; v3==u2) return 0; else if (v1==u2 &amp;&amp; v2==u1 &amp;&amp; v3==u3) return 0; else if (v1==u2 &amp;&amp; v2==u3 &amp;&amp; v3==u1) return else if (v1==u3 &amp;&amp; v2==u2 &amp;&amp; v3==u1) return 0; else if (v1==u3 &amp;&amp; v2==u1 &amp;&amp; v3==u2) return 0; else return 1; } </code></pre> <p>this function determines if a triplet is a multiple of a triplet </p> <pre class="lang-c prettyprint-override"><code>// This function tests if the triplet &lt;p,q,r&gt; is an exact multiple of &lt;l,m,n&gt; in any order // (arrangement/permutation) int are_exact_multiples(int v1, int v2, int v3, int u1, int u2, int u3) { if (v1%u1==0 &amp;&amp; v2%u2==0 &amp;&amp; v3%u3==0) return 1; else if (u1%v1==0 &amp;&amp; u2%v2==0 &amp;&amp; u3%v3==0) return 1; else if (v1%u1==0 &amp;&amp; v2%u3==0 &amp;&amp; v3%u2==0) return 1; else if (u1%v1==0 &amp;&amp; u2%v3==0 &amp;&amp; u3%v2==0) return 1; else if (v1%u2==0 &amp;&amp; v2%u1==0 &amp;&amp; v3%u3==0) return 1; else if (v1%u2==0 &amp;&amp; v2%u3==0 &amp;&amp; v3%u1==0) return 1; else if (u1%v2==0 &amp;&amp; u2%v1==0 &amp;&amp; u3%v3==0) return 1; else if (u1%v2==0 &amp;&amp; u2%v3==0 &amp;&amp; u3%v1==0) return 1; else if (v1%u3==0 &amp;&amp; v2%u2==0 &amp;&amp; v3%u1==0) return 1; else if (v1%u3==0 &amp;&amp; v2%u1==0 &amp;&amp; v3%u2==0) return 1; else if (u1%v3==0 &amp;&amp; u2%v2==0 &amp;&amp; u3%v1==0) return 1; else if (u1%v3==0 &amp;&amp; u2%v1==0 &amp;&amp; u3%v2==0) return 1; else return 0; } </code></pre> <p>I know that the algorithm is not optimized... i will do that later. Can somebody please help me get this code to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload