Fibonacci setup

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Postby Guest » Mon Dec 06, 2004 11:18 pm

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? :?
Guest
 

Postby Guest » Mon Dec 06, 2004 11:32 pm

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? :?

Yes it would be possible to change it to do that.
You will need a prime number generator would be the biggest thing missing.

Almost the same, um not really in my opinion.

Just let us know what we can help you with.
Guest
 

Postby Guest » Mon Dec 06, 2004 11:48 pm

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



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 << " ";
}
}
}
Guest
 

Postby . » Tue Dec 07, 2004 12:14 am

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 << "    ";
                 }
      }   
}

I assume that you want the prime factorization?
If you do then,
27 = 3 x 3 x 3
It works out like this:
2 divides into 27 = 13.5
um no that is not it, it must be even remainder so we try the next prime:
3 divided into 27 = 9
Okay we have a even remainder so we take that again and try 3 once more:
3 divided into 9 = 3
okay that is even so we keep that now:
3 divided into 3 = 1
so the prime factorization?
27 = 3 x 3 x 3
we used 3 three times.
Does this help?
I also assume you do not want me to give code outright?
.
 

Postby Guest » Tue Dec 07, 2004 8:02 am

So.... I need to use a prime loop?
I've already got a prime loop, but it's a different function.


That code I gave above should work though, shouldn't it? 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?
Guest
 

Postby . » Tue Dec 07, 2004 9:31 am

Anonymous wrote:So.... I need to use a prime loop? I've already got a prime loop, but it's a different function.

You will some way to generate prime numbers to test with. So yes you can use your other function for this.
Anonymous wrote:That code I gave above should work though, shouldn't it?

No, you have not included a way to check if 3 divides into 9 again ( assumed we are still using 27 as a example ) after the first division. There are others things that make it not work but one thing at a time.
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?

Well maybe not what you want but it does print what you ask it to.
Code: Select all
            if (x * y == a)
                cout << x << "    " << y << "    ";



What are using the int returned from your function for???
Code: Select all
int factors(int a)
{




You might want to look at the "Modulus Operator".
http://msdn.microsoft.com/library/en-us ... rators.asp
Example:
Code: Select all
#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;
}
.
 

Postby Guest » Tue Dec 07, 2004 12:07 pm

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;
}
Guest
 

Postby Guest » Tue Dec 07, 2004 12:26 pm

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; 
}

You are almost there.
But if
Code: Select all
if (test==0)

is true then you should check to see if, if is true again and to do this you should not increment
Code: Select all
y++;

and you should also change the value of "a" if test == 0 is true you should divide a into y in the if statement. And only increment y when the else executes.
Guest
 

Postby . » Tue Dec 07, 2004 12:48 pm

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;
}

Also you should try to use the code tags to preserve the formatting of your code to make it easier to read. Take a look here:
viewtopic.php?p=62841&highlight=#62841

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

Take a look at this does this not look much better formatting?
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;
}



On a side note why are you using functions to do this. If it is to learn functions. Then you should change your functions return type or add a return statement to the function. If you are note receiving a compiler error for no return statement for your function with no return statement you should get a more up to date compiler?
.
 

Postby Guest » Tue Dec 07, 2004 4:12 pm

Yes, it is to learn how to use functions. We are actually told not to put any cin's out cout's in the functions. We're suppose to write another function to call the function, this pass the the user entered to the function. I don't know how to do that just yet. But i'm learning.

I changed it to a for loop, and did a minor change. It seems to be working like how I want it. And, nope, I am not getting a compiler error, I have the lastest version of bloodshed. What about it now??

int factors(int a)
{
int y= 1;
int test;

for( y ; y <= a ; y++)
{
test= a % y;
if (test == 0)
{
cout<< y << " ";
}
}
cout<< '\n' ;
}
Guest
 

Postby t i l e x » Tue Dec 07, 2004 5:42 pm

I didn't compile any of the two, that's why.
User avatar
t i l e x
 
Posts: 3602
Joined: Wed Dec 03, 2003 3:59 pm
Location: Québec (Canada)

Postby Guest » Tue Dec 07, 2004 5:51 pm

Um the last bit of code was closer. You can use a for or a while loop. I prefer a while loop for this code, but that is personal preference.
My last post was kind of cryptic. To get the the prime factors it works out like this:
Example input : 27
first prime to use is 2
2%27 ==5 //because it only gives remainder of division of 13.5
so if we where to test for this in a "if statement" when this is false then a good place to increment "y" to the next prime would be in a "else statement" and you would want to control the incrementing of "y" so you would only want to increment in the else statement.

no so now increment to next prime 3
3%27 =0 yes because we have no remainder, but we need the results of division for the next try but we also need to test the results with 3 again so some where in your code you need to actually divide 3/27 and save 9. but you only want to divide if there is no remainder so a "if" would be good place to take care of this.
now we have the last prime used which was 3 and the results of division which is 9
3%9=0 yes 3 is a factor of 9 because we have no remainder.
so we divide and save the results of that division which is 3 and we save the prime number we where using to test the new results of division
3%3=0 so as usual we divide and are left with zero so we are done.
So basically you can conclude from this with just a few changes to this code you are there.
Code: Select all
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;
}
Guest
 

Postby Alvaro » Tue Dec 07, 2004 6:11 pm

Darrylsh wrote:
t i l e x wrote:
*EDIT* Wow, that is close to being nearly close of looking somewhat like Alvaro code. :wink:


Naw, here is Alvaro Fibonacci code.

:)

Some code even more in my style:
Code: Select all
#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;
}



It uses a faster algorithm. Can anybody figure out how it works?
User avatar
Alvaro
Moderator
 
Posts: 5180
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Blueteeth » Wed Dec 08, 2004 3:58 am

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. :wink:

Actually Fibonacci sequence is the 1st example in "Why we need dynamic programming" :)
User avatar
Blueteeth
 
Posts: 610
Joined: Thu Sep 25, 2003 9:54 pm
Location: Egypt

Postby Guest » Wed Dec 08, 2004 2:57 pm

Alvaro wrote:It uses a faster algorithm. Can anybody figure out how it works?

I am trying, but I am slow. So I will look for a little longer before I give a shot to explain. But I will probably not even be close?
Guest
 

PreviousNext

Return to For Beginners

Who is online

Users browsing this forum: Ask Jeeves [Bot] and 1 guest

cron