mirror of https://github.com/TheAlgorithms/C
Merge pull request #335 from Nikhil-Bhat6/ArmstrongNumber
Added ArmstrongNumber.c
This commit is contained in:
commit
eb5bdc5449
|
@ -75,19 +75,20 @@ C
|
|||
|
||||
|
||||
## Misc
|
||||
- ArmstrongNumber
|
||||
- Binning
|
||||
- Factorial
|
||||
- Fibonacci
|
||||
- Greatest Common Divisor
|
||||
- isArmstrong
|
||||
- LongestSubSequence
|
||||
- palindrome
|
||||
- prime factorization
|
||||
- QUARTILE
|
||||
- rselect
|
||||
- strongNumber
|
||||
- TowerOfHanoi
|
||||
- Greatest Common Divisor
|
||||
- Sudoku Solver
|
||||
- prime factorization
|
||||
- Sudoku Solver
|
||||
- TowerOfHanoi
|
||||
|
||||
## Project Euler
|
||||
- Problem 1
|
||||
|
|
|
@ -32,6 +32,8 @@ LeetCode
|
|||
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c)|Easy|
|
||||
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|
||||
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium|
|
||||
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c)|Easy|
|
||||
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c)|Easy|
|
||||
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy|
|
||||
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy|
|
||||
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
uint32_t reverseBits(uint32_t n) {
|
||||
uint TotalBits = 32;
|
||||
uint32_t reverse_int = 0; //stored in memory as 32 bits, each bit valued 0
|
||||
uint i;
|
||||
for(i = 0; i < TotalBits; i++) {
|
||||
if((n & (UINT32_C(1) << i))) //if the bit on the ith position of 32 bit input is 1, then proceed
|
||||
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, since just 1 is treated as int which cannot be shifted left more than 30 times
|
||||
reverse_int = reverse_int | (UINT32_C(1) << (TotalBits - 1 - i)); //Convert the ith bit from the end in reverse_int from 0 to 1, if ith bit from beginning in n is 1
|
||||
//This is achieved by using bitwise OR on reverse_int (where ith bit from end is currently 0) and
|
||||
//1 shifted left 31 - i bits (to ith bit from the end)
|
||||
}
|
||||
return reverse_int;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
int hammingWeight(uint32_t n) {
|
||||
int TotalBits = 32;
|
||||
int i, weight = 0;
|
||||
for(i = 0; i < TotalBits; i++) {
|
||||
if(n & (UINT32_C(1) << i)) //if the bit on the ith position of 32 bit input is 1, then proceed
|
||||
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, as just 1 is treated as int which cannot be shifted left more than 30 times
|
||||
weight += 1;
|
||||
}
|
||||
return weight;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//A number is called as Armstrong number if sum of cubes of digits of number is equal to the number itself.
|
||||
// For Example 153 is an Armstrong number because 153 = 1³+5³+3³.
|
||||
#include <stdio.h>
|
||||
|
||||
//Function to calculate x raised to the power y
|
||||
int power(int x, unsigned int y)
|
||||
{
|
||||
if (y == 0)
|
||||
return 1;
|
||||
if (y % 2 == 0)
|
||||
return power(x, y / 2) * power(x, y / 2);
|
||||
return x * power(x, y / 2) * power(x, y / 2);
|
||||
}
|
||||
|
||||
//Function to calculate order of the number
|
||||
int order(int x)
|
||||
{
|
||||
int n = 0;
|
||||
while (x) {
|
||||
n++;
|
||||
x = x / 10;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// Function to check whether the given number is
|
||||
// Armstrong number or not
|
||||
int isArmstrong(int x)
|
||||
{
|
||||
// Calling order function
|
||||
int n = order(x);
|
||||
int temp = x, sum = 0;
|
||||
while (temp)
|
||||
{
|
||||
int r = temp % 10;
|
||||
sum += power(r, n);
|
||||
temp = temp / 10;
|
||||
}
|
||||
|
||||
// If satisfies Armstrong condition
|
||||
if (sum == x)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
int main()
|
||||
{
|
||||
int x = 153;
|
||||
if (isArmstrong(x) == 1)
|
||||
printf("True\n");
|
||||
else
|
||||
printf("False\n");
|
||||
|
||||
x = 1253;
|
||||
if (isArmstrong(x) == 1)
|
||||
printf("True\n");
|
||||
else
|
||||
printf("False\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue