C/Przykłady z komentarzem: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Nie podano opisu zmian
Linia 378:
 
</syntaxhighlight>
===Liczby wymierne binarne===
<syntaxhighlight lang=c>
 
#include <stdio.h> // fprintf
 
 
 
/*
https://stackoverflow.com/questions/19738919/gcd-function-for-c
The GCD function uses Euclid's Algorithm.
It computes A mod B, then swaps A and B with an XOR swap.
*/
 
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
 
a = b;
b = temp;
}
return a;
}
 
 
 
int main(){
 
int dMax = 6;
int denominator = 1;
fprintf(stdout, "0/%d\n", denominator); // initial value
for (int d = 0; d < dMax; ++d ){
denominator *=2;
for (int n = 1; n < denominator; ++ n ){
if (gcd(n,denominator)==1 )// irreducible fraction
{fprintf(stdout, "%d/%d\t", n,denominator);}
}// n
fprintf(stdout, "\n"); // end of the line
} // d
return 0;
}
</syntaxhighlight>
 
Kompilujemy:
 
gcc d.c -Wall -Wextra
Wynik :
<syntaxhighlight lang=bash>
./a.out
0/1
1/2
1/4 3/4
1/8 3/8 5/8 7/8
1/16 3/16 5/16 7/16 9/16 11/16 13/16 15/16
1/32 3/32 5/32 7/32 9/32 11/32 13/32 15/32 17/32 19/32 21/32 23/32 25/32 27/32 29/32 31/32
1/64 3/64 5/64 7/64 9/64 11/64 13/64 15/64 17/64 19/64 21/64 23/64 25/64 27/64 29/64 31/64 33/64 35/64 37/64 39/64 41/64 43/64 45/64 47/64 49/64 51/64 53/64 55/64 57/64 59/64 61/64 63/64
 
</syntaxhighlight>
 
 
{{BrClear}}