1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Oksi-84 [34.3K]
2 years ago
5

Write a recursive method named digitSum that accepts an integer as a parameter and returns the sum of its digits. For example, c

alling digitSum(1729) should return 1 7 2 9, which is 19. If the number is negative, return the negation of the value. For example, calling digitSum(-1729) should return -19.
Computers and Technology
1 answer:
harkovskaia [24]2 years ago
4 0

Answer:

The function in Python is as follows:

def digitSum( n ):

if n == 0:

 return 0

if n>0:

    return (n % 10 + digitSum(int(n / 10)))

else:

    return -1 * (abs(n) % 10 + digitSum(int(abs(n) / 10)))

Explanation:

This defines the method

def digitSum( n ):

This returns 0 if the number is 0

<em> if n == 0: </em>

<em>  return 0 </em>

If the number is greater than 0, this recursively sum up the digits

<em> if n>0: </em>

<em>     return (n % 10 + digitSum(int(n / 10))) </em>

If the number is lesser than 0, this recursively sum up the absolute value of the digits (i.e. the positive equivalent). The result is then negated

<em> else: </em>

<em>     return -1 * (abs(n) % 10 + digitSum(int(abs(n) / 10)))</em>

You might be interested in
Pleaseeee help me w this!
wariber [46]
If you can click on 2 images the modem and cable would be the answers if not your best bet would be modem. And yes I can help you with other questions on this.
3 0
3 years ago
Assume that passwords are selected from four-character combinations of 26 alphabeticcharacters. Assume that an adversary is able
tigry1 [53]

Answer:

for a) the time required is 228488.5 seconds= 63.469 hours

for b) the time required is 54 seconds

Explanation:

for a) since each combination is equally probable , then the number of possible combinations is

CT=Combinations = number of characters^length of password = 26⁴

then the number of combinations at time t will be the total , less the ones already tried:

Ct = CT - (n-1) , since n=α*t  → Ct=CT-α*t  

since each combination is equally probable , then the probability to succeed  

pt = 1/Ct = 1/ (CT- α*t +1)

but the probability of having a success in time t , means also not succeeding in the previous trials , then

Pt = pt*П(1-pk), for k=1 to t-1

Pt = 1/ (CT- α*t +1) П[1-1/ (CT- α*k +1)] = 1/ (CT- α*t +1) П[(CT- α*k )] /(CT- α*k +1)]  

since α=1 ,  

Pt = 1/ (CT- t +1) П[(CT- k )] /(CT- k +1)] = 1/ (CT- t +1)  * [CT- (t-1) ]/CT  =  1/CT

then the expected value of the random variable t= time to discover the correct password is

E(t) = ∑ t* Pt = ∑ t *1/CT , for t=1 until t=CT/α =CT

E(t) = ∑ t *(1/CT) = (1/CT) ∑ t = (1/CT) * CT*(CT+1)/2 = (CT+1)/2

therefore

E(t) = (CT+1)/2 = (26⁴ +1)/2 = 228488.5 seconds = 63.469 hours

for b)

time required = time to find character 1 + time to find character 2 +time to find character 3 +time to find character 4 = 4*time to find character

since the time to find a character is the same case as before but with CT2=Combinations = 26 ,then

t= 4*tc

E(t) = 4*E(tc) = 4*(CT2+1)/2 = 4*(26+1)/2 = 54 seconds

6 0
3 years ago
New and just need help with C coding. I've tried if statements and it outputs the wrong number.
dimaraw [331]

Answer:

#include <stdio.h>

int main(void) {

 

 int num1;

 int num2;

 int num3;

 printf("Enter three integers: ");

 scanf("%d", &num1);

 scanf("%d", &num2);

 scanf("%d", &num3);

 if (num1 == 0 || num2 == 0 || num3 == 0)

 {

   printf("please input a number greater than zero :)\n");

 }

 if (num1 <= num2 && num1 <= num3)

 {

   printf("%i is the smallest number!\n", num1);

 }

 else if (num2 <= num1 && num2 <= num3)

 {

   printf("%i is the smallest number!\n", num2);

 }

 else

 {

   printf("%i is the smallest number!\n", num3);

 }

 return 0;

}

Explanation:

Alright so let's start with the requirements of the question:

  1. must take 3 integers from user input
  2. determine which of these 3 numbers are the smallest
  3. spit out the number to out

So we needed to create 3 variables to hold each integer that was going to be passed into our script.

By using scanf("%i", &variableName) we were able to take in user input and  store it inside of num1, num2, and num3.

Since you mentioned you were new to the C programming language, I threw in the first if statement as an example of how they can be used, use it as a guide for future reference, sometimes it's better to understand your code visually.

Basically what this if statement does is, it checks to see if any of the integers that came in from user input was the number zero, it told the user it does not accept that number, please input a number greater than zero.

if (num1 == 0 || num2 == 0 || num3 == 0)

 {

   printf("please input a number greater than zero :)\n");

 }

I used this methodology and implemented the heart of the question,

whichever number is smaller, print it out on the shell (output).

if (num1 <= num2 && num1 <= num3)

^^ here we're checking if the first variable we created is smaller than the second variable and the third ^^

 {

   printf("%i is the smallest number!\n", num1);

   ^^ if it is smaller, then print integer and then print a new line so the next line looks neat ^^

   ( incase if your wondering what "\n" is, its a special character that allows you so print a new line on the terminal, kind of like hitting the return or enter key )

 }

else if (num2 <= num1 && num2 <= num3)

^^ else if is used when your checking for more than one thing, and so for the second variable we checked to see if it was smaller than the first and third variable we created ^^

 {

   printf("%i is the smallest number!\n", num2); < -- and we print if it's smaller

 }

Last but not least:

else

^^ if it isn't num1 or num2, then it must be num3 ^^

 {

   printf("%i is the smallest number!\n", num3);

  we checked the first two options, if its neither of those then we have only one variable left, and thats num3.

 }

I hope that helps !!

Good luck on your coding journey :) ‍

7 0
2 years ago
Read 2 more answers
When enter a function or formula in a cell, which is the character you must type?
vladimir1956 [14]
The answer is A. For instance, to use the sum function, you would need to =SUM()
7 0
3 years ago
What is your F O R T N I T E name
kolbaska11 [484]

Answer:

don't have name, but would be something cool.

Explanation:

poor, have a splendid day.

6 0
2 years ago
Read 2 more answers
Other questions:
  • Which is a credit card balance A. The amount of interest you must pay the credit card company B. The required minimum payment to
    5·1 answer
  • for what reason do some security professionals consider insiders more dangerous than outside intruders?
    8·1 answer
  • Why is a networked system a benefit?
    7·1 answer
  • When looking at a relationship between two tables on an ERD, the _____ table can be identified by the presence of a foreign key
    8·1 answer
  • A web application is an example of:
    7·1 answer
  • Public class Square extends Shape{
    13·1 answer
  • A program that allows employees to choose their starting and ending times, as long as they are at work during a specified core p
    8·1 answer
  • Here is the problem specification: An Internet service provider has three different subscription packages for its customers: Pac
    5·1 answer
  • Which code will print a random number between 1 and 100?
    15·2 answers
  • ANSWER ASAP!!!!!!!!!!!!!
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!