Question

1. An integer is said to be perfect if the sum of its factors, including 1,...

1. An integer is said to be perfect if the sum of its factors, including 1, is equal to the number itself. Write a Prolog predicate perfect(N, F), which determines if integer N is perfect and if so, return it’s list of factors F. If N is not perfect, the predicate fails. You may find the arithmetic operation M mod N to be useful. Test your predicate on the following.

• perfect(6, Factors) ⇒ Factors = [1, 2, 3]

• perfect(298, Factors) ⇒ no

• perfect(496, Factors) ⇒ Factors = [1, 2, 4, 8, 16, 31, 62, 124, 248]

2. Write a Prolog predicate delete(Atom, List1, List2) that deletes all occurrences, no matter how deep, of Atom in List1 and returns the result in List2. The returned list cannot contain anything in place of the deleted atoms. Test your predicate on the following.

• delete(a, [a, b, r, a, c, a, d, a, b, r, a], List) ⇒ List = [b, r, c, d, b, r]

• delete(b, [no, bs, here], List) ⇒ List = []

• delete(nest, [nest, [second, nest, level], [third, [nest], level], [[[big, nest]]]], List) ⇒ [[second, level], [third, [], level], [[[big]]]]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

program for perfect number

#include <stdio.h> 
#include <conio.h>

int main()
{
    int a, num, sum = 0 ;
    printf("Enter a number to check for the perfect number : ") ;
    scanf("%d", &num) ;
    
    for(a = 1 ; a < num ; a++)
    {
        if(num % a == 0)
        sum = sum + a ;
    }

    if (sum == num)
    printf("\n%d is a perfect number", num) ;
    else
    printf("\n%d is not a perfect number", num) ;
    getch() ;
}

2)

predicate delete(Atom, List1, List2) that deletes all occurrences, no matter how deep, of Atom in List1 and returns the result in List2. The returned list cannot contain anything in place of the deleted atoms. Test your predicate on the following.

• delete(a, [a, b, r, a, c, a, d, a, b, r, a], List) ⇒ List = [b, r, c, d, b, r]

• delete(b, [no, bs, here], List) ⇒ List = []

• delete(nest, [nest, [second, nest, level], [third, [nest], level], [[[big, nest]]]], List) ⇒ [[second, level], [third, [], level], [[[big]]]]

Success:
   [eclipse]: delete(X,[1,M,X],L), writeln((M,X,L)), fail.
   _g66 , 1 , [_g66, 1]
   _g66 , _g66 , [1, _g66]
   _g66 , _g72 , [1, _g66]
   no (more) solution.

   [eclipse]: delete(3,[1,3,5,3],L).
   L = [1, 5, 3]    More? (;)
   L = [1, 3, 5]
   yes.

   [eclipse]: delete(X,L,[a,b]), writeln((X,L)), fail.
   _g66 , [_g66, a, b]
   _g66 , [a, _g66, b]
   _g66 , [a, b, _g66]
   no (more) solution.

   delete(X,[1,2],L).   (gives X=1 L=[2]; X=2 L=[1]).
Fail:
   delete(1,[1,2,1,3],[2,3]).
Add a comment
Answer #2
#include <iostream> #include <cctype>using namespace std; int main(){
    int n,i=1,sum=0;
    cout << "Enter a number: ";
    cin >> n;
       while(i<n){
       if(n%i==0)
       sum=sum+i;
       i++; }
 if(sum==n)
    cout << i << " is a perfect number\n"; else
    cout << i << " is not a perfect number\n";
    system("pause"); return 0;}


answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
1. An integer is said to be perfect if the sum of its factors, including 1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • An integer number is said to be a perfect number if its factors, including 1 (but...

    An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1+2+3. Write a method isPerfect that determines if parameter number is a perfect number. Use this method in a test program to display all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect.

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

  • A positive integer n is “perfect” if the sum of its positive factors, excluding itself, equals...

    A positive integer n is “perfect” if the sum of its positive factors, excluding itself, equals n.  Write a perfect function in Haskell that takes a single integer argument and returns the list of all perfect numbers up to that argument. Report all of the perfect numbers up to 1000 (i.e. call 1000)

  • An integer is said to be a perfect number if the sum of its divisors, including...

    An integer is said to be a perfect number if the sum of its divisors, including 1(but not the number itself), is equal to the number. For example, 6 is a perfect number because 6 = 1+2+3. A) Write a function numPerfect( number ) that returns true when the number is a perfect number, false when it is not.        B) Write a C# console program that calls the function in A) to determine and print all the perfect numbers...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per...

    JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per Share Amounts) (Note 1)* 2016 71,890 21,789 50.101 20,067 9.143 29 Sales to customers Cost of products sold Gross profit Selling, marketing and administrative expenses Research and development expense In-process research and development Interest income Interest expense, net of portion capitalized (Note 4) Other (income) expense, net Restructuring (Note 22) Eamings before provision for taxes on income Provision for taxes on income (Note 8)...

  • JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per...

    JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per Share Amounts) (Note 1)* 2016 71,890 21,789 50.101 20,067 9.143 29 Sales to customers Cost of products sold Gross profit Selling, marketing and administrative expenses Research and development expense In-process research and development Interest income Interest expense, net of portion capitalized (Note 4) Other (income) expense, net Restructuring (Note 22) Eamings before provision for taxes on income Provision for taxes on income (Note 8)...

  • JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per...

    JOHNSON & JOHNSON AND SUBSIDIARIES CONSOLIDATED STATEMENTS OF EARNINGS (Dollars and Shares in Millions Except Per Share Amounts) (Note 1)* 2016 71,890 21,789 50.101 20,067 9.143 29 Sales to customers Cost of products sold Gross profit Selling, marketing and administrative expenses Research and development expense In-process research and development Interest income Interest expense, net of portion capitalized (Note 4) Other (income) expense, net Restructuring (Note 22) Eamings before provision for taxes on income Provision for taxes on income (Note 8)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT