Question

In: Computer Science

SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum...

SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum of its positive divisors except the number itself. The first two perfect numbers are 6 and 28 since 1+2+3=6 and 1+2+ 4+7+14=28. Write a matlab computer program that finds the first n perfect number (the user must input the value of n) and show them in a vector

thanks! xo

Solutions

Expert Solution

Code Screenshot :

Executable Code:

%Prompting the use for input
n = input('Please enter the value of n: ');
%Calling the function
perfectNumbers(n)
%Function definition
function result = perfectNumbers(n)
%Declaring a vector
result = zeros(1,n);
%Iterating till n
for i = 1:n
   %Finding the perfect numbers
   test = 1:i-1;
   if (sum(test(mod(i,test) == 0)) == i)
       result(i) = i;
   end
end
result(result == 0) = [];
end

Sample Output :

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
An Armstrong number is an integer such that the sum of the cubes of its digits...
An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Write a C function to print all Armstrong numbers between a given interval. Then write a C program to keep reading two integers and print all Armstrong numbers between these two integers by calling that function.
Matlab question: By using while loop to solve, Sum up bottles until you are over 75....
Matlab question: By using while loop to solve, Sum up bottles until you are over 75. How many days/loops are needed? Print out cups on the shelf and total until you reach at least 75. Finish the code below, least_num_Cups = randi([75, 300], 1,1); while totalCups = % Change this code to reflect change in totalCups fprintf('%0.0f cups of tea on the shelf, total %0.0f\n',); k = ; % Change code to update k end k_complete = ; % Change...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
a. Write a function sumDigits that takes a positive integer value and returns the total sum...
a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. Sample Runs: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a positive...
A formula with a positive integer (less than 32 bits) and a positive decimal (number with...
A formula with a positive integer (less than 32 bits) and a positive decimal (number with decimal points) is expressed in the median formula. Change the given median to postfix and write a program that outputs the results of the calculation. operand ::= positive integer or positive error Positive integer ::= A number expressed as less than 32 bits consisting of 0 to 9. Positive integer representation of 0, 0100, 00934, 1056, 65535 is allowed Positive decimal ::= Positive integer...
//Java Language Read an integer number from the user. If the number is not positive, change...
//Java Language Read an integer number from the user. If the number is not positive, change its sign.    1   Count the digits of the number 2   Count the odd digits of the number 3   Calculate the sum of the digit values of the number
Use strong induction to show that every positive integer, n, can be written as a sum...
Use strong induction to show that every positive integer, n, can be written as a sum of powers of two: 20, 21, 22, 23, .....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT