Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 35: | Line 35: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp> |
<lang cpp> |
||
⚫ | |||
Contructor taking two intergers representing a rational number |
|||
int nextTerm returns the next term of the continued fraction |
|||
bool moreTerms returns true if ther are more terms |
|||
⚫ | |||
*/ |
|||
#include <iostream> |
#include <iostream> |
||
/* Interface for all Continued Fractions |
|||
⚫ | |||
*/ |
|||
class ContinuedFraction { |
class ContinuedFraction { |
||
private: |
private: |
||
⚫ | |||
public: |
public: |
||
virtual const int nextTerm(){}; |
|||
virtual const bool moreTerms(){}; |
|||
}; |
|||
/* Create a continued fraction from a rational number |
|||
Nigel Galloway, February 9th., 2013. |
|||
*/ |
|||
class r2cf : ContinuedFraction { |
|||
⚫ | |||
public: |
|||
r2cf(const int numerator, const int denominator) { |
|||
n1 = numerator; n2 = denominator; |
n1 = numerator; n2 = denominator; |
||
} |
} |
||
Line 56: | Line 59: | ||
return thisTerm; |
return thisTerm; |
||
} |
} |
||
const bool moreTerms() { |
const bool moreTerms() {return n2 > 0;} |
||
}; |
|||
return n2 > 0; |
|||
⚫ | |||
} |
|||
Nigel Galloway, February 9th., 2013. |
|||
*/ |
|||
class SQRT2 : public ContinuedFraction { |
|||
private: bool first=true; |
|||
public: |
|||
const int nextTerm() {if (first) {first = false; return 1;} else return 2;} |
|||
const bool moreTerms() {return true;} |
|||
}; |
}; |
||
</lang> |
</lang> |
||
==Testing== |
===Testing=== |
||
===1/2 3 23/8 13/11 22/7=== |
====1/2 3 23/8 13/11 22/7==== |
||
<lang cpp> |
<lang cpp> |
||
int main() { |
int main() { |
||
for( |
for(r2cf n(1,2); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
std::cout << std::endl; |
||
for( |
for(r2cf n(3,1); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
std::cout << std::endl; |
||
for( |
for(r2cf n(23,8); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
std::cout << std::endl; |
||
for( |
for(r2cf n(13,11); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
std::cout << std::endl; |
||
for( |
for(r2cf n(22,7); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
std::cout << std::endl; |
||
return 0; |
return 0; |
||
Line 79: | Line 89: | ||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
|||
⚫ | |||
0 2 |
|||
3 |
|||
2 1 7 |
|||
1 5 2 |
|||
3 7 |
|||
</pre> |
|||
⚫ | |||
<lang cpp> |
<lang cpp> |
||
int main() { |
int main() { |
||
int i = 0; |
|||
⚫ | |||
for(SQRT2 n; i++ < 20; std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
std::cout << std::endl; |
||
for( |
for(r2cf n(14142,10000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
||
std::cout << std::endl; |
|||
⚫ | |||
std::cout << std::endl; |
std::cout << std::endl; |
||
return 0; |
return 0; |
||
Line 90: | Line 110: | ||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
|||
⚫ | |||
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 |
|||
1 2 2 2 2 2 1 1 29 |
|||
1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 |
|||
</pre> |
|||
⚫ | |||
<lang cpp> |
<lang cpp> |
||
int main() { |
int main() { |
||
for(r2cf n(31,10); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(314,100); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(3142,1000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(31428,10000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(314285,100000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(3142857,1000000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(31428571,10000000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
for(r2cf n(314285714,100000000); n.moreTerms(); std::cout << n.nextTerm() << " "); |
|||
std::cout << std::endl; |
|||
return 0; |
|||
} |
} |
||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
|||
3 10 |
|||
3 7 7 |
|||
3 7 23 1 2 |
|||
3 7 357 |
|||
3 7 2857 |
|||
3 7 142857 |
|||
3 7 476190 3 |
|||
3 7 7142857 |
|||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
Straightforward implementation: |
Straightforward implementation: |