Informatik für Mathematiker und Physiker HS16 Solutions 4

Lecturer: Prof. B. Gärtner
Informatik für Mathematiker und Physiker
HS16
Solutions 4
Course URL: http://lec.inf.ethz.ch/ifmp/2016/
Solution 1
a) Output:
5 4 3 2 1
b) Output: 4 11 12 1 5
Notice that the number 0 - which ends the loop - is not output anymore.
c) Output:
2 6 10
d) Output:
(1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3)
Solution 2
Please keep in mind that once again there are multiple ways to solve each subtask. Provided below
are example code snippets you can insert in each Codeboard template that solve the respective
task. Also given is a short explanation for why the particular loops are chosen.
a) A while-loop works well here as we don’t need a variable that is used only in the loop, i.e.
we don’t need a loop-specific init-statement.
// I n p u t
unsigned int n ;
std : : cin >> n ;
// Computation o f t h e r e d u c e d number
while ( n >= 1 0 0 0 )
n −= 1 0 0 0 ;
// Output
std : : cout << n << "\n" ;
Remark: The result is the same as for n % 1000.
b) A for-loop works well because the summation-index i iterates through the numbers {1, 2, ..., n}
but it is not used after the loop anymore (the sum’s value is used afterwards).
1
// I n p u t
unsigned int n ;
std : : cin >> n ;
// Computation o f t h e sum
unsigned int sum = 0 ;
for ( unsigned int i = 1 ; i <= n ; ++i )
sum += i∗i ;
// Output
std : : cout << sum << "\n" ;
c) A do-loop works well because the loop-condition depends on the things we repeatedly do (we
verify a condition based on the input), i.e. we have to get an input first before we can verify
it.
unsigned int input ;
unsigned int counter = 0 ;
do {
std : : cin >> input ;
++counter ;
} while ( input != 0 ) ;
std : : cout << counter << "\n" ;
d) Two nested for-loops do the job elegantly here. The outer one indicates the row index and
the inner one then prints the stars based on the row index from the outer loop.
// I n p u t
unsigned int n ;
std : : cin >> n ;
// Output
for ( unsigned int i = 1 ; i <= n ; ++i ) {
for ( unsigned int j = 0 ; j < i ; ++j )
std : : cout << "*" ;
std : : cout << "\n" ;
}
Solution 3
1
2
// I n f o r m a t i k − S e r i e 4 − Aufgabe 3
// Programm : k d i v i s o r s . cpp
2
3
4
5
6
// A u t o r : . . . ( Gruppe . . . )
// O u t p u t s t h e numbers i n t h e r a n g e [ 1 , 1 0 0 0 ] w i t h e x a c t l y k d i v i s o r s
//#i n c l u d e ” t e s t s . h” // remove s l a s h e s a t b e g i n n i n g o f l i n e t o t e s t ←or submit
7 #include <i o s t r e a m >
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main ( )
{
// i n p u t
unsigned int k ;
std : : cin >> k ;
// l o o p o v e r a l l numbers i n t h e r a n g e [ 1 , 1 0 0 0 ]
for ( unsigned int n = 1 ; n <= 1 0 0 0 ; ++n ) {
// c o u n t t h e number o f d i v i s o r s o f n
unsigned int divisors = 0 ;
for ( unsigned int m = 1 ; m <= n ; ++m )
if ( n % m == 0 )
++divisors ;
// o u t p u t n i f t h e number o f d i v i s o r s i s e q u a l t o k
if ( divisors == k )
std : : cout << n << " " ;
}
std : : cout << "\n" ;
return 0 ;
}
Solution 4
1
2
3
4
5
6
//
//
//
//
I n f o r m a t i k − S e r i e 4 − Aufgabe 4
Programm : p i . cpp
A u t o r : . . . ( Gruppe . . . )
Approximate p i a c c o r d i n g to f i r s t n terms o f both f o r m u l a s .
//#i n c l u d e ” t e s t s . h” // remove s l a s h e s a t b e g i n n i n g o f l i n e t o t e s t ←or submit
7 #include <i o s t r e a m >
8
9
10
11
int main ( )
{
// i n p u t
3
unsigned int n ;
std : : cin >> n ;
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
// c o m p u t a t i o n − f o r m u l a 1
double pif1 = 0 . 0 ;
for ( unsigned int i = 1 ; i < 2∗n ; i += 2 )
if ( i % 4 == 1 )
// c a s e : 1/1 + 1/5 + 1/9 + . . .
pif1 += 4 . 0 / i ;
else
// c a s e : −1/3 − 1/7 − 1/11 − . . .
pif1 −= 4 . 0 / i ;
// c o m p u t a t i o n − f o r m u l a 2
// a u x i l i a r y v a r i a b l e s
// i n i t i a l i z e d f o r f i r s t term o f f o r w a r d sum ( i =0)
double numer = 2 . 0 ; // n u m e r a t o r i −t h term
double denom = 1 . 0 ; // d e n o m i n a t o r i −t h term
double pif2 = 2 . 0 ; // v a l u e a f t e r term i ( i =0 i n i t i a l l y , t h e n i ←= 1 , 2 , . . . , n−1)
for ( unsigned int i = 1 ; i < n ; ++i ) {
numer ∗= i ;
denom ∗= ( 2 ∗ i + 1 ) ;
pif2 += numer / denom ; // u p d a t e t o term i
}
// o u t p u t
std : : cout << "Formula 1: " << pif1 << "\n" ;
std : : cout << "Formula 2: " << pif2 << "\n" ;
return 0 ;
}
Notice that when running the first formula for n = 10, 000, it gives on our platform the
approximation 3.14149 (still off in the fourth digit after the decimal point). For n = 100, 000,
we get 3.14158 (still off in the fifth digit after the decimal point). For n = 1, 000, 000, finally,
the result is correct to five digits after the decimal point: 3.14159.
On the other hand, the approximation based on the second formula already yields the result
3.14159 for n = 17 on our platform, so this version is obviously preferable.
4