Fibonacci Modified
Implement a modified Fibonacci sequence using the following definition:
Given terms and where , term is computed as:
Given three integers, , , and , compute and print the term of a modified Fibonacci sequence.
Example
Return .
Function Description
Complete the fibonacciModified function in the editor below. It must return the number in the sequence.
fibonacciModified has the following parameter(s):
- int t1: an integer
- int t2: an integer
- int n: the iteration to report
Returns
- int: the number in the sequence
Note: The value of may far exceed the range of a -bit integer. Many submission languages have libraries that can handle such large results but, for those that don't (e.g., C++), you will need to compensate for the size of the result.
Input Format
A single line of three space-separated integers, the values of , , and .
Constraints
- may far exceed the range of a -bit integer.
Sample Input
0 1 5
Sample Output
5
Explanation
The first two terms of the sequence are and , which gives us a modified Fibonacci sequence of . The term is .
xxxxxxxxxx
char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);
int parse_int(char*);
/*
* Complete the 'fibonacciModified' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER t1
* 2. INTEGER t2
* 3. INTEGER n
*/
int fibonacciModified(int t1, int t2, int n) {
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char** first_multiple_input = split_string(rtrim(readline()));
int t1 = parse_int(*(first_multiple_input + 0));
int t2 = parse_int(*(first_multiple_input + 1));
int n = parse_int(*(first_multiple_input + 2));
int result = fibonacciModified(t1, t2, n);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}