-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodingProblem4b.pg
More file actions
96 lines (74 loc) · 2.23 KB
/
CodingProblem4b.pg
File metadata and controls
96 lines (74 loc) · 2.23 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
##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 = 5;
#Allow the student to generate a new (re-randomized) problem
#AFTER they have submitted a correct answer.
ProblemRandomize(onlyAfterDue=>0);
##############################################################
$N = random(1000000,60000000,1);
$f1 = 1;
$f2 = 1;
$S = 1;
for ( $j = 1; $j < 40; $j++) {
if ($f2/2 != floor($f2/2) && $f2<$N){$S = $S + $f2;}
$f1temp = $f2;
$f2 = $f1 + $f2;
$f1 = $f1temp;
}
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting the sequence with \(a_0 = 1\) and \(a_1 = 1\) the first 10 terms will be $BR \(1,1,2,3,5,8,13,21,34,55, ...\)
$BR
Consider only the terms of the Fibonacci sequence whose values do not exceed $N. Write computer code (with MATLAB or other similar programming language) to find the sum of all odd-valued terms in the sequence.
$BR
Sum = \{ans_rule(15)\}
$PAR
END_TEXT
BEGIN_HINT
To solve this problem you will likely need a "for-loop" to build the Fibonacci sequence. You will also need an "if" statement to check that a Fibonacci number is even
$BR
To check if a number \(k\) is even we would check to see if \(k / 2\) is equal to \(floor(k/2)\) using an "if" statement.
END_HINT
ANS(num_cmp($S,,tol=>.5));
##############################################################
#
# Answers
#
#
ENDDOCUMENT();