Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fibonacci square sum last digit #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Mathematics_Algo/fibonacci_sum_squares.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>

//Computes the last digit of Fibonacci series square sum- F0^2 + F1^2 + · · · + Fn^2 in constant time.
long long fibonacci_fast(int n)
{

long long f[n];
f[0] = 0;
f[1] = 1;

for (int i = 2; i <= n; i++)
{
f[i] = (f[i - 1] + f[i - 2]) % 10;
}

return f[n];
}
long long fib_square(long long n)
{
if (n < 1)
return 0;
long long x = fibonacci_fast(n % 60);
long long y = fibonacci_fast((n + 1) % 60);
long long z = (x * y) % 10;
return z;
}
int main()
{
long long n = 0;
std::cin >> n;
std::cout << fib_square(n);
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The purpose of this repository is to get all the Algorithms required for Competi
13) totient :- Total numbers from 1 to N whose gcd with N is 1.
14) fibonacciNumber(n) :- Returns the nth Fibonacci Number in constant time.
15)Spiral Search in 2d array (C++ code)
16) Computes the last digit of fibonacci square series F0^2 + F1^2 + · · · + Fn^2 in constant time.

## String_Algo
1) isVowel :- Check whether a character is vowel or not.
Expand All @@ -34,3 +35,8 @@ The purpose of this repository is to get all the Algorithms required for Competi
1)Spiral search in 2d array


## Trees_Algo
1) LCA.cpp
2) Topview.cpp
3) Bottomview.cpp
4) Common_tree_traversal.cpp
111 changes: 111 additions & 0 deletions Trees_Algo/common_tree_traversal_methods.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;

struct node
{
int data;
struct node *left;
struct node *right;
};

struct node *newnode(int data)
{
struct node *p = new (struct node);
//struct node* p = (struct node*)malloc(sizeof(struct node));

p->data = data;
p->left = NULL;
p->right = NULL;
return p;
}

//Inorder (Left, Root, Right)
//Preorder (Root, Left, Right)
//Postorder (Left, Right, Root)
//Levelorder
int height(node *root)
{
// Write your code here.
if (root == NULL)
{
return 0;
}
if (root->left == NULL && root->right == NULL)
{
return 0;
}
return std::max(height(root->left) + 1, height(root->right) + 1);
}

int printlevel(struct node *root, int level)
{
if (root == NULL)
return 0;
if (level == 0)
cout << root->data << " ";
else
{
printlevel(root->left, level - 1);
printlevel(root->right, level - 1);
}
}

void levelOrder(struct node *root)

{
int h = height(root);
for (int i = 0; i <= h; i++)
{
printlevel(root, i);
}
}

int preorder(struct node *root)
{
if (root == NULL)
return 0;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

int inorder(struct node *root)
{
if (root == NULL)
return 0;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
int postorder(struct node *root)
{
if (root == NULL)
return 0;
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}

int main()
{
struct node *root = newnode(3);
root->left = newnode(7);
root->right = newnode(5);
root->right->right = newnode(11);
root->left->left = newnode(9);

printf("\nTree traversed in preorder=\n");
preorder(root);
printf("\nTree traversed in inorder=\n");
inorder(root);
printf("\nTree traversed in postorder=\n");
postorder(root);
printf("\nTree traversed in levelorder=\n");
levelOrder(root);

return 0;
}