Question

In: Computer Science

A zombie picks up a calculator and starts adding odd whole numbers together, in order: 1...

A zombie picks up a calculator and starts adding odd whole numbers together, in order: 1 + 3 + 5......etc. You want to be able to determine the last number the zombie will add that will make the sum exceed some target number, such as "What will be the last number added that will make the sum on his calculator greater than 10,000?".

Write the MATLAB code necessary to solve this problem for the zombie or he will eat your brain. Create a function with an input of the target number (in the scenario given above the target number would be 10,000), which outputs the last numbered added.

Solutions

Expert Solution

MATLAB Function:

function f = oddSum(n)
num=1
total=1
while 1
num = num + 2
total = total + num
if total>n
break
end
end
f = num
end

If the above function is called using oddSum(10000), the below output will come.

Output:

num =  1
total =  1
num =  3
total =  4
num =  5
total =  9
num =  7
total =  16
num =  9
total =  25
num =  11
total =  36
num =  13
total =  49
num =  15
total =  64
num =  17
total =  81
num =  19
total =  100
num =  21
total =  121
num =  23
total =  144
num =  25
total =  169
num =  27
total =  196
num =  29
total =  225
num =  31
total =  256
num =  33
total =  289
num =  35
total =  324
num =  37
total =  361
num =  39
total =  400
num =  41
total =  441
num =  43
total =  484
num =  45
total =  529
num =  47
total =  576
num =  49
total =  625
num =  51
total =  676
num =  53
total =  729
num =  55
total =  784
num =  57
total =  841
num =  59
total =  900
num =  61
total =  961
num =  63
total =  1024
num =  65
total =  1089
num =  67
total =  1156
num =  69
total =  1225
num =  71
total =  1296
num =  73
total =  1369
num =  75
total =  1444
num =  77
total =  1521
num =  79
total =  1600
num =  81
total =  1681
num =  83
total =  1764
num =  85
total =  1849
num =  87
total =  1936
num =  89
total =  2025
num =  91
total =  2116
num =  93
total =  2209
num =  95
total =  2304
num =  97
total =  2401
num =  99
total =  2500
num =  101
total =  2601
num =  103
total =  2704
num =  105
total =  2809
num =  107
total =  2916
num =  109
total =  3025
num =  111
total =  3136
num =  113
total =  3249
num =  115
total =  3364
num =  117
total =  3481
num =  119
total =  3600
num =  121
total =  3721
num =  123
total =  3844
num =  125
total =  3969
num =  127
total =  4096
num =  129
total =  4225
num =  131
total =  4356
num =  133
total =  4489
num =  135
total =  4624
num =  137
total =  4761
num =  139
total =  4900
num =  141
total =  5041
num =  143
total =  5184
num =  145
total =  5329
num =  147
total =  5476
num =  149
total =  5625
num =  151
total =  5776
num =  153
total =  5929
num =  155
total =  6084
num =  157
total =  6241
num =  159
total =  6400
num =  161
total =  6561
num =  163
total =  6724
num =  165
total =  6889
num =  167
total =  7056
num =  169
total =  7225
num =  171
total =  7396
num =  173
total =  7569
num =  175
total =  7744
num =  177
total =  7921
num =  179
total =  8100
num =  181
total =  8281
num =  183
total =  8464
num =  185
total =  8649
num =  187
total =  8836
num =  189
total =  9025
num =  191
total =  9216
num =  193
total =  9409
num =  195
total =  9604
num =  197
total =  9801
num =  199
total =  10000
num =  201
total =  10201
f =  201

Hence, the function result is 201 which makes the sum greater than 10000.


Related Solutions

There is a special calculator that when adding numbers rounds the number to the closest integer....
There is a special calculator that when adding numbers rounds the number to the closest integer. For example, 1.1 + 2 + 3.6 will be calculated as 1 + 2 + 4. The error from each addition follows a uniform distribution of (-0.5, 0.5). a. When 1500 numbers are added, what is the probability that the absolute value of the total error is greater than 15? b. How many numbers can be added until the probability of the absolute value...
There is a special calculator that when used to add numbers up, it always rounds up...
There is a special calculator that when used to add numbers up, it always rounds up or down the number to the closest integer and ignore the decimals. For example, you want to add 1.1+2+3.6, it will show can calculate as 1+2+4, etc. Assume the error from each adding follows uniform distribution on (-0.5, 0.5). When adding 1500 numbers together, what is the probability of the absolute value of total error is greater than 15? How many numbers can you...
Java Program 1Use a loop to add up the odd numbers between 100 and 200. 2Use...
Java Program 1Use a loop to add up the odd numbers between 100 and 200. 2Use a loop to determine if a number is prime. Recall: a number is prime if its only factors are 1 and itself. 3. Nested Loops: }Write a nested loop that finds the largest prime number smaller than 125.
5) In the MegaMillions lottery a player buys a $1 ticket and picks five numbers from...
5) In the MegaMillions lottery a player buys a $1 ticket and picks five numbers from the numbers 1 to 70. The lottery has a bin with seventy white balls, each with a number from 1 to 70. The lottery picks five balls from the bin with the white balls. If the five numbers the player picked match the numbers drawn on the five white balls then the player wins $1,000,000 (in most states, but not including California, where the...
Prove the following statements! 1. There is a bijection from the positive odd numbers to the...
Prove the following statements! 1. There is a bijection from the positive odd numbers to the integers divisible by 3. 2. There is an injection f : Q→N. 3. If f : N→R is a function, then it is not surjective.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Use a For loop to compute the sum of all the odd numbers from 1 through...
Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum...
In question 1, do not use any properties of odd and even numbers outside of their...
In question 1, do not use any properties of odd and even numbers outside of their definitions. However, for questions 2 and 3, you can use the following parity results without having to prove them each time. For any integers a and b, the product a.b is odd if and only if a and b are both odd. For any integers, a and b, the sum a + b is even if and only if a and b have the...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT