Almost the same kind of principle isn't it?
Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard
Anonymous wrote:Okay, lemme ask ya this. Would it be possible to modifiy this code to create a function that finds the factors of a number entered?
Almost the same kind of principle isn't it?
Anonymous wrote:int a is the number passed (entered by user). It works, but it prints out too many things.
For example. If int a = 27, it needs to print out : 1 3 9 27
- Code: Select all
int factors(int a)
{
for(int x = 1; x <= a; x++)
{
for(int y = (a / 2); y > 0; y--)
{
if (x * y == a) cout << x << " " << y << " ";
}
}
}
Anonymous wrote:So.... I need to use a prime loop? I've already got a prime loop, but it's a different function.
Anonymous wrote:That code I gave above should work though, shouldn't it?
Anonymous wrote:I think it prints out 3 9 9 27 3 1 something like that. It's out of order, and umm.... prints an extra 3 and 9 somehow?
if (x * y == a)
cout << x << " " << y << " ";int factors(int a)
{#include <iostream>
using namespace std;
int main()
{
int N =27;
int a=2;
int b=0;
int c=0;
b= a%N;
c=a/N;
cout << "The remainder from division of " << N << " is " << b
<< "\nDivision of " << N << " is " << c << endl;
cin.get();//so window will stay open
return 0;
}Anonymous wrote:how about this? It seems to work, minus printing " 1 "
- Code: Select all
int factors1(int a)
{
int y=2;
while (y<=a)
{
int test;
test=a%y;
if (test==0)
{
cout<<y<<" ";
y++;
}
else
y++;
}
cout<<endl;
}
if (test==0) y++;Anonymous wrote:how about this? It seems to work, minus printing " 1 "
int factors1(int a)
{
int y=2;
while (y<=a)
{
int test;
test=a%y;
if (test==0)
{
cout<<y<<" ";
y++;
}
else
y++;
}
cout<<endl;
}
Anonymous wrote:But any way if you look at this link you can see what code tags should look like before you hit submit you should also use the preview button to make sure it looks okay.
posting.php?mode=quote&p=63397
int factors1(int a)
{
int y=2;
while (y<=a)
{
int test;
test=a%y;
if (test==0)
{
cout<<y<<" ";
y++;
}
else
y++;
}
cout<<endl;
}int factors1(int a)
{
int y=2;
while (y<=a)
{
int test;
test=a%y;
if (test==0)
{
cout<<y<<" ";
a=a/y;
// y++;
}
else
y++;//only increment here
}
cout<<endl;
}Darrylsh wrote:t i l e x wrote:
*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code.
Naw, here is Alvaro Fibonacci code.
#include <iostream>
template <typename T>
struct FibMatrix {
T a, b;
FibMatrix(T a, T b=0):a(a),b(b){}
FibMatrix operator*(FibMatrix const &s) const{
return FibMatrix(a*s.a+b*s.b,a*s.b+b*(s.a+s.b));
}
};
template <typename T>
T power(T const &x, unsigned n){
return n?(n&1?x*power(x,n-1):power(x*x,n>>1)):T(1);
}
template <typename T>
T fib(unsigned n){
return power(FibMatrix<T>(0,1),n).b;
}
int main(void){
for(int i=2;i<20;++i)
std::cout << i << ' ' << fib<double>(i) << std::endl;
}
t i l e x wrote:The other function is a recursive version of the one above. Try this:
- Code: Select all
#include <iostream>
int nb_fib; // assumed you will give it a value
void Fibonnacci(int N, int a = 1, int b = 1) {
if(N) {
std::cout << a;
if(!(N % nb_fib))
std::cout << std::endl;
Fibonnacci(b, a+b, --N);
}
std::cout << std::endl << "End of the fibonnacci sequence.";
}
int main(void) {
std::cout << "Enter the number of fibonnacci numbers to output on each line: ";
std::cin >> nb_fib;
int N;
std::cout << "Enter the number of fibonnacci numbers to generate: ";
std::cin >> N;
Fibonnacci(N);
return 0;
}
*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code.
Users browsing this forum: Ask Jeeves [Bot] and 1 guest