-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatzConjectureWhileLoop.pg
More file actions
99 lines (81 loc) · 2.65 KB
/
CollatzConjectureWhileLoop.pg
File metadata and controls
99 lines (81 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
##DESCRIPTION
## This problem is meant to have students write brief pieces of code to solve problems that have a specific numerical answer.
##ENDDESCRIPTION
##KEYWORDS('programming', 'code')
## DBsubject('Programming')
## DBchapter('')
## DBsection('')
## Date('6/7/2016')
## Author('')
## Institution('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
########################################################################
DOCUMENT();
loadMacros(
"PG.pl",
"PGauxiliaryFunctions.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"MathObjects.pl",
"problemRandomize.pl",
"PGanswermacros.pl",
"PGgraphmacros.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl",
"answerHints.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
# How many attempts before a hint?
$showHint = 10;
#Allow the student to generate a new (re-randomized) problem
#AFTER they have submitted a correct answer.
ProblemRandomize(onlyAfterDue=>0);
##############################################################
$A0 = random(5000,10000,1);
$A = $A0;
$C = 0;
do {
$C = $C+1;
if ($A/2 == floor($A/2) )
{$A = $A / 2;}
else
{$A = 3*$A + 1;}
} until ($A <= 1);
$StopTime = $C;
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
$PAR
The $BBOLD Collatz Conjecture $EBOLD in mathematics can be summarized as follows: $BR
Let \( a_0 \) be any postive integer and build the sequence $BR
\( a_{n+1} = \frac{a_n}{2} \quad \text{ if } a_n \text{ is divisible by 2 } \) $BR
\( a_{n+1} = 3 a_n + 1 \quad \text{ if } a_n \text{ is not divisible by 2.} \) $BR
The Collatz Conjecture states that with any starting point, this sequence will evenetually reach the number 1. This statement appears to be true for every starting point that can be tested, but there is still no formal mathematical proof for why this occurs. $PAR
The smallest \( n \) such that \( a_n = 1\) is called the "stopping time" for an intitial condition. For example, let's consider the Collatz sequence starting at \( a_0 = 6. \) $BR
\( a_0 = 6, \quad a_1 = 3, \quad a_2 = 10, \quad a_3 = 5, \quad a_4 = 16, \quad a_5 = 8, \quad a_6 = 4, \quad a_7 = 2, \quad a_8 = 1 \) $BR
and therefore the stopping time for \( a_0 = 6\) is \( 8\).
$PAR
$BBOLD Now for your coding challenge: $EBOLD $PAR
Write MATLAB code to find the stopping time for \( a_0 = $A0 \). $BR
Stopping Time = \{ans_rule(15)\}
$PAR
END_TEXT
BEGIN_HINT
You will likely need a while loop and an if-then statement for this problem.
END_HINT
ANS(num_cmp($StopTime,,tol=>.5));
##############################################################
#
# Answers
#
#
ENDDOCUMENT();