Skip to content
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
55 changes: 55 additions & 0 deletions keith.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// C++ program to check if a number is Keith or not

#include<bits/stdc++.h>
using namespace std;

// Returns true if x is Keith, else false.
bool isKeith(int x)
{
// Store all digits of x in a vector "terms"
// Also find number of digits and store in "n".
vector <int> terms;
int temp = x, n = 0; // n is number of digits in x
while (temp > 0)
{
terms.push_back(temp%10);
temp = temp/10;
n++;
}

// To get digits in right order (from MSB to
// LSB)
reverse(terms.begin(), terms.end());

// Keep finding next trms of a sequence generated
// using digits of x until we either reach x or a
// number greate than x
int next_term = 0, i = n;
while (next_term < x)
{
next_term = 0;

// Next term is sum of previous n terms
for (int j=1; j<=n; j++)
next_term += terms[i-j];

terms.push_back(next_term);
i++;
}

/* When the control comes out of the while loop,
either the next_term is equal to the number
or greater than it.
If next_term is equal to x, then x is a
Keith number, else not */
return (next_term == x);
}

// Driver program
int main()
{
isKeith(14)? cout << "Yes\n" : cout << "No\n";
isKeith(12)? cout << "Yes\n" : cout << "No\n";
isKeith(197)? cout << "Yes\n" : cout << "No\n";
return 0;
}
31 changes: 31 additions & 0 deletions lucas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Iterative C/C++ program
// to find n'th Lucas Number
#include <stdio.h>

// Iterative function
int lucas(int n)
{
// declaring base values
// for positions 0 and 1
int a = 2, b = 1, c, i;

if (n == 0)
return a;

// generating number
for (i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}

// Driver Code
int main()
{
int n = 9;
printf("%d", lucas(n));
return 0;
}
27 changes: 27 additions & 0 deletions pell number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Iterative Pell Number Series in C
#include <stdio.h>

// calculate nth pell number
int pell(int n)
{
if (n <= 2)
return n;

int a = 1;
int b = 2;
int c, i;
for (i = 3; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}

// driver function
int main()
{
int n = 4;
printf("%d", pell(n));
return 0;
}
27 changes: 27 additions & 0 deletions pronic number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// C/C++ program to check if a number is pronic or not

#include<bits/stdc++.h>
using namespace std;

// function to check Pronic Number
bool pronic_check(int n)
{
int x = (int)(sqrt(n));

// Checking Pronic Number by
// multiplying consecutive numbers
if (x*(x+1)==n)
return true;
else
return false;
}

// Driver Code
int main(void)
{
int n = 56;
pronic_check(n) == true? cout << "YES" :
cout << "NO";

return 0;
}