Search
 
SCRIPT & CODE EXAMPLE
 

CPP

armstrong number

from math import log10

def is_armstrong(num):
    """
    This function checks if num is an Armstrong number.
    Note that an Armstrong number is a n-digit number 
    that is equal to the sum of each of its digits 
    raised to the nth power.

    For example:  1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 
    371, 407, and 1634 are all Armstrong numbers

    Time complexity: O(n^2)
    Space complexity: O(1)
    """
    if num < 0:
        return False
    if num == 0:
        return True
    n = int(log10(num)) + 1
    sum = 0
    temp = num
    while temp > 0:
        last_digit = temp % 10
        sum += last_digit ** n
        temp //= 10
    return num == sum

print(is_armstrong(3))  # True
print(is_armstrong(120))  # False
print(is_armstrong(153))  # True
print(is_armstrong(1600))  # False
print(is_armstrong(1634))  # True
Comment

armstrong number

sum of cubes of the digits
Comment

armstrong numbers

// Armstrong Numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748...
Comment

armstrong number

// ARMSTRONG NUMBER
// 153

let sum = 0;
const number = 153;
// create a temporary variable
let temp = number;
while (temp > 0) {
  // finding the one's digit
  let remainder = temp % 10;

  sum += remainder * remainder * remainder;

  // removing last digit from the number
  temp = parseInt(temp / 10); // convert float into integer
}
// check the condition
if (sum == number) {
  console.log(`${number} is an Armstrong number`);
} else {
  console.log(`${number} is not an Armstrong number.`);
}
Comment

Armstrong number

// C++ Program to find
// Nth Armstrong Number
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
 
// Function to find Nth Armstrong Number
int NthArmstrong(int n)
{
    int count = 0;
 
    // upper limit from integer
    for (int i = 1; i <= INT_MAX; i++) {
        int num = i, rem, digit = 0, sum = 0;
 
        // Copy the value for num in num
        num = i;
 
        // Find total digits in num
        digit = (int)log10(num) + 1;
 
        // Calculate sum of power of digits
        while (num > 0) {
            rem = num % 10;
            sum = sum + pow(rem, digit);
            num = num / 10;
        }
        // Check for Armstrong number
        if (i == sum)
            count++;
        if (count == n)
            return i;
    }
}
 
// Driver Function
int main()
{
    int n = 12;
    cout << NthArmstrong(n);
    return 0;
}
 
// This Code is Contributed by 'jaingyayak'
Comment

armstrong number function

def is_armstrong_number(number: int)-> bool:
    arm = str(number)
    lenght = len(arm)
    sum = 0
    for digit in arm:
        sum += int(digit)**lenght
    return sum == number
Comment

PREVIOUS NEXT
Code Example
Cpp :: Tricky Subset Problem 
Cpp :: 191. Number of 1 Bits leetcode solution in c++ 
Cpp :: online compiler c++ with big O calculatorhttps://www.codegrepper.com/code-examples/cpp/how+to+convert+string+to+wchar_t+in+c%2B%2B 
Cpp :: Browse Folder Dialog, Search Folder and All Sub Folders using C/C++ 
Cpp :: how to merge string array in C++ 
Cpp :: inside information subtask 2 
Cpp :: find the number of digits of a given integer n . 
Cpp :: c++ set value to inf 
Cpp :: program in c++ for simple interest rate 
Cpp :: 01matrix 
Cpp :: declare static table filled cpp 
Cpp :: cf 633b trivial problem explanation 
Cpp :: how to convert malloc function to cpp 
Cpp :: play sound opencv video c++ 
Cpp :: ue4 foreach loop c++ 
Cpp :: how to delay text in c++ console app 
Cpp :: constructor init list 
Cpp :: c++ format number thousands separator 
Cpp :: C++ concept simple requirements 
Cpp :: MPI_File_seek 
Cpp :: e.cpp 
Cpp :: prime template c++ 
Cpp :: print all chrchetrs of a string c++ 
Cpp :: c++ string to vector int 
Cpp :: int to string Using to_string method 
Cpp :: c++ for loops 
Cpp :: frequency of characters in a string in c++ 
Cpp :: how to implement binders and decorators on c++ lik python? 
C :: how to store a user input with spaces in c 
C :: haskell print 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =