1

I'm making a program like Fibonacci series, but for numbers up to the value provided by user, example: 0 to 5 (0, 1, 2, 3, 4). Program needs to calculate the sum of numbers, move to right and calculate other five numbers. It's working for Fibonacci series, but not for higher numbers. Does anyone know how to do this?

#include <stdio.h>


int main() {

    int gg; //number which user inputs
    int dg = 0; //first number
    int next = 0;
    int n;
    int i;

    printf("Number of series: ");
    scanf("%d", &gg);

    printf("Positive integer: ");//max number which outputs in printf("Series: \n", gg), etc. 1000;
    scanf("%d", &n);

    printf("Series: \n", gg); //result

    for (i = 0; i < gg; i++) {
        printf("%d, ", i);
    }

    next = gg + dg;

    while(next <= n){
        printf("%d, ", next);
        dg = gg;
        gg = next;
        next = gg + dg;
    }

    return 0;
}

For values 0 to 5 it should output:

0, 1, 2, 3, 4, 10, 20, 39, 76, 149, 294...

But it outputs:

0, 1, 2, 3, 4, 5, 10, 15, 25, 40, 65, 105, 170, 275,
5
  • Your code clearly doesn't match your actual output. Commented Mar 23, 2019 at 17:00
  • 2
    Could you post a full Minimal Complete Verifiable Example? Commented Mar 23, 2019 at 17:02
  • The first 12 numbers in Fibonacci series are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 (or did I misunderstand your question?) Commented Mar 23, 2019 at 17:12
  • 1
    What is printf("Series: \n", gg); supposed to do? And what is dg? Are you posting typos, or ignoring compiler warnings? Commented Mar 23, 2019 at 17:27
  • If you want a sequence that's like the Fibonacci sequence, but summing the previous k numbers instead of just two, you need to keep track of more than two numbers. Commented Mar 23, 2019 at 18:39

1 Answer 1

1

You need save the last nth values, not just the last two values. In your example you need to store the last five values. You can use an array for this:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int series_number;
    int *previous_numbers;
    int marker = 0;
    int max_series_value;
    int next = 0;

    printf("Number of series: ");
    scanf("%d", &series_number);

    printf("Positive integer: ");
    scanf("%d", &max_series_value);

    printf("Series %d: \n", series_number);

    previous_numbers = malloc(sizeof *previous_numbers *series_number);
    for (int i = 0; i < series_number; i++) {
        printf("%d, ", i);
        previous_numbers[i] = i;
        next += i;
    }
    previous_numbers[marker] = next;
    marker = (marker+1) % series_number;

    while(next <= max_series_value){
        printf("%d, ", next);
        next = 0;
        for (int i = 0; i < series_number; i++) {
            next += previous_numbers[(marker+i) % series_number];
        }
        previous_numbers[marker] = next;
        marker = (marker+1) % series_number;
    }
    free(previous_numbers);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.