# Lab 2 COMP9021 solution

\$19.99

Original Work
Category:

## Description

5/5 - (1 vote)

1 Perfect numbers
Anumberisperfectifitisequaltothesumofitsdivisors,itselfexcluded. Forinstance,thedivisors of 28 distinct from 28 are 1, 2, 4, 7 and 14, and 1+2+4+7+14=28, hence 28 is perfect. Write a program that outputs all 3-digit perfect numbers. You should ﬁnd out that here is only one solution, equal to 496.
2 R Finding special triples of the form (n,n+1,n+2)
Write a program that ﬁnds all triples of consecutive positive three-digit integers each of which is the sum of two squares, that is, all triples of the form (n,n+1,n+2) such that:
• n, n+1 and n+2 are integers at least equal to 100 and at most equal to 999; • each of n, n+1 and n+2 is of the form a2 +b2.
Hint: As we are not constrained by memory space for this problem, we might use a list that stores an integer for all indexes n in [100,999], equal to 1 in case n is the sum of two squares, and to 0 otherwise. Then it is just a matter of ﬁnding three consecutive 1’s in the list. This idea can be reﬁned (by not storing 1s, but suitable nonzero values) to not only know that some number is of the form a2 +b2, but also know such a pair (a,b)… The output of the program could be:
(144, 145, 146) (equal to (0^2+12^2, 8^2+9^2, 5^2+11^2)) is a solution. (232, 233, 234) (equal to (6^2+14^2, 8^2+13^2, 3^2+15^2)) is a solution. (288, 289, 290) (equal to (12^2+12^2, 8^2+15^2, 11^2+13^2)) is a solution. (360, 361, 362) (equal to (6^2+18^2, 0^2+19^2, 1^2+19^2)) is a solution. (520, 521, 522) (equal to (14^2+18^2, 11^2+20^2, 9^2+21^2)) is a solution. (576, 577, 578) (equal to (0^2+24^2, 1^2+24^2, 17^2+17^2)) is a solution. (584, 585, 586) (equal to (10^2+22^2, 12^2+21^2, 15^2+19^2)) is a solution. (800, 801, 802) (equal to (20^2+20^2, 15^2+24^2, 19^2+21^2)) is a solution. (808, 809, 810) (equal to (18^2+22^2, 5^2+28^2, 9^2+27^2)) is a solution.
1
3 Estimating the probabilities of hypotheses in the light of more and more evidence
Write a program that simulates the cast of an unknown die, chosen from a set of 5 dice with 4, 6, 8, 12, and 20 faces. To start with, every die has a probability of 0.2 to be the chosen die. At every cast, the probability of each die being the chosen die is updated using Bayes’ rule (ﬁnd out about it if you do not know it, it is simple…). The probabilities are displayed for at most 5 casts. If more than 5 casts have been requested, the ﬁnal probabilities obtained for the chosen number of casts are eventually displayed. Here is a possible output:
Enter the desired number of times a randomly chosen die will be cast: 2
This is a secret, but the chosen die is the one with 4 faces
The die that has been cast yields: 4 The updated dice probabilies are: 4: 37.04% 6: 24.69% 8: 18.52% 12: 12.35% 20: 7.41%
The die that has been cast yields: 1 The updated dice probabilies are: 4: 54.18% 6: 24.08% 8: 13.55% 12: 6.02% 20: 2.17%
2
Here is another possible output:
Enter the desired number of times a randomly chosen die will be cast: 5
This is a secret, but the chosen die is the one with 8 faces
The die that has been cast yields: 7 The updated dice probabilies are: 4: 0.00% 6: 0.00% 8: 48.39% 12: 32.26% 20: 19.35%
The die that has been cast yields: 1 The updated dice probabilies are: 4: 0.00% 6: 0.00% 8: 62.33% 12: 27.70% 20: 9.97%
The die that has been cast yields: 1 The updated dice probabilies are: 4: 0.00% 6: 0.00% 8: 73.51% 12: 21.78% 20: 4.70%
The die that has been cast yields: 3 The updated dice probabilies are: 4: 0.00% 6: 0.00% 8: 81.76% 12: 16.15% 20: 2.09%
The die that has been cast yields: 7 The updated dice probabilies are: 4: 0.00% 6: 0.00% 8: 87.57% 12: 11.53% 20: 0.90%
3
Here is still another possible output:
Enter the desired number of times a randomly chosen die will be cast: 20
This is a secret, but the chosen die is the one with 6 faces
The die that has been cast yields: 1 The updated dice probabilies are: 4: 37.04% 6: 24.69% 8: 18.52% 12: 12.35% 20: 7.41%
The die that has been cast yields: 4 The updated dice probabilies are: 4: 54.18% 6: 24.08% 8: 13.55% 12: 6.02% 20: 2.17%
The die that has been cast yields: 6 The updated dice probabilies are: 4: 0.00% 6: 63.54% 8: 26.80% 12: 7.94% 20: 1.72%
The die that has been cast yields: 6 The updated dice probabilies are: 4: 0.00% 6: 72.10% 8: 22.81% 12: 4.51% 20: 0.58%
The die that has been cast yields: 5 The updated dice probabilies are: 4: 0.00% 6: 78.68% 8: 18.67% 12: 2.46% 20: 0.19%
The final probabilities are: 4: 0.00% 6: 99.68% 8: 0.32% 12: 0.00% 20: 0.00%