filmov
tv
Python: Harshad or Niven Number | Python Interview Questions | DE Interview Questions | Data Science
![preview_player](https://i.ytimg.com/vi/NUeHFLpG3-0/maxresdefault.jpg)
Показать описание
Video 157: In this video, we'll explore how to find the given number is a Harshad number or not.
An integer divisible by the sum of its digits is said to be a Harshad number.
You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
In this video, we delve into the concept of Harshad numbers, which are integers divisible by the sum of their digits. We explore three different Python approaches to determine if a given number is a Harshad number or not.
*** Approach 1: General Looping Logic ***
In this approach, we use a simple loop to iterate through the digits of the given number, calculate their sum, and then check if the number is divisible by this sum. If it is divisible, we return the sum of the digits; otherwise, we return -1.
*** Approach 2: Using List Comprehension ***
Here, we leverage the power of list comprehension to succinctly sum the digits of the given number. We calculate the sum of digits using a list comprehension and then check if the number is divisible by this sum. If divisible, we return the sum of the digits; otherwise, we return -1.
*** Approach 3: Using While Loop ***
In this approach, we employ a while loop to iteratively extract digits from the given number, calculate their sum, and then check if the number is divisible by this sum. We continue this process until we have processed all the digits. If the number is divisible by the sum of its digits, we return the sum; otherwise, we return -1.
For a comprehensive understanding and practical demonstration, be sure to watch the entire video. Happy coding!
Follow me on,
#recursive #countingalgorithm #list #looping #listmanipulation #uniquesubstring #longestsubstring #slicing #lineartimecomplexity #python #pythonquestions #pythontest #pythonprogramming #pythontutorial #python3 #pythonforbeginners #interviewquestions #interview #dataengineers #deinterview #pythoninterview #interviewquestions #leetcode #placementpreparation #meta #google #facebook #apple #netflix #amazon #google #faang #maanga #dataengineers #alphanumeric #nonalphanumeric #growwithdata #set #split #remove #pythonfunctions #tamil #tamilpython #tamilinterview #tamilinterviewlatest #PythonInterviewQuestions #PythonInterviewQuestionsAndAnswers #ImportantPythonInterviewQuestions #PrepareForPythonJobs #PythonJobInterviewPreparation #TopPythonInterviewQuestions #CommonPythonInterviewQuestions #growwithdata
An integer divisible by the sum of its digits is said to be a Harshad number.
You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
In this video, we delve into the concept of Harshad numbers, which are integers divisible by the sum of their digits. We explore three different Python approaches to determine if a given number is a Harshad number or not.
*** Approach 1: General Looping Logic ***
In this approach, we use a simple loop to iterate through the digits of the given number, calculate their sum, and then check if the number is divisible by this sum. If it is divisible, we return the sum of the digits; otherwise, we return -1.
*** Approach 2: Using List Comprehension ***
Here, we leverage the power of list comprehension to succinctly sum the digits of the given number. We calculate the sum of digits using a list comprehension and then check if the number is divisible by this sum. If divisible, we return the sum of the digits; otherwise, we return -1.
*** Approach 3: Using While Loop ***
In this approach, we employ a while loop to iteratively extract digits from the given number, calculate their sum, and then check if the number is divisible by this sum. We continue this process until we have processed all the digits. If the number is divisible by the sum of its digits, we return the sum; otherwise, we return -1.
For a comprehensive understanding and practical demonstration, be sure to watch the entire video. Happy coding!
Follow me on,
#recursive #countingalgorithm #list #looping #listmanipulation #uniquesubstring #longestsubstring #slicing #lineartimecomplexity #python #pythonquestions #pythontest #pythonprogramming #pythontutorial #python3 #pythonforbeginners #interviewquestions #interview #dataengineers #deinterview #pythoninterview #interviewquestions #leetcode #placementpreparation #meta #google #facebook #apple #netflix #amazon #google #faang #maanga #dataengineers #alphanumeric #nonalphanumeric #growwithdata #set #split #remove #pythonfunctions #tamil #tamilpython #tamilinterview #tamilinterviewlatest #PythonInterviewQuestions #PythonInterviewQuestionsAndAnswers #ImportantPythonInterviewQuestions #PrepareForPythonJobs #PythonJobInterviewPreparation #TopPythonInterviewQuestions #CommonPythonInterviewQuestions #growwithdata