english improved
This commit is contained in:
@@ -11,22 +11,22 @@ julia:
|
||||
using InteractiveUtils
|
||||
```
|
||||
|
||||
# A small Example Program
|
||||
# A Small Example Program
|
||||
|
||||
## What Skills Are Needed for Programming?
|
||||
|
||||
|
||||
- **Thinking in algorithms:**
|
||||
What steps are required to solve the problem? What data must be stored, what data structures must be created? What cases can occur and must be recognized and handled?
|
||||
- **Implementation of the algorithm into a program:**
|
||||
- **Implementing the algorithm in a program:**
|
||||
What data structures, constructs, functions... does the programming language provide?
|
||||
- **Formal syntax:**
|
||||
Humans understand 'broken English'; computers don't understand 'broken C++' or 'broken Julia'. The syntax must be mastered.
|
||||
- **The "ecosystem" of the language:**
|
||||
How do I run my code? How do the development environments work? What options does the compiler understand? How do I install additional libraries? How do I read error messages? Where can I get information?
|
||||
- **The art of debugging:**
|
||||
Programming beginners are often happy when they have eliminated all syntax errors and the program finally 'runs'. For more complex programs, the work only just begins, as testing and finding and fixing errors in the algorithm must now be done.
|
||||
- **Sense of the efficiency and complexity of algorithms**
|
||||
Programming beginners are often happy when they have eliminated all syntax errors and the program finally 'runs'. For more complex programs, the work only just begins, as testing, finding, and fixing errors in the algorithm must now be done.
|
||||
- **Intuition for the efficiency and complexity of algorithms**
|
||||
- **Specifics of computer arithmetic**, especially floating point numbers
|
||||
|
||||
These cannot all be mastered at once. Be patient with yourself and 'play' with the language.
|
||||
@@ -81,7 +81,7 @@ for i in 1:999 # loop
|
||||
s += i # add to accumulator
|
||||
end # end test
|
||||
end # end loop
|
||||
println(" The answer is: $s") # output result
|
||||
println("The answer is: $s") # output result
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -118,21 +118,21 @@ How many starting numbers below ten million will arrive at 89?
|
||||
:::
|
||||
|
||||
Programs can be developed [*top-down* and *bottom-up*](https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design).
|
||||
*Top-down* would probably mean here: "We start with a loop `for i = 1:9999999`." The other way leads over individually testable components and is often more effective. (And with a certain project size, one approaches the goal from both 'top' and 'bottom' simultaneously.)
|
||||
*Top-down* would probably mean here: "We start with a loop `for i = 1:9999999`." The other approach works through individually testable components and is often more effective. (And with a certain project size, one approaches the goal from both 'top' and 'bottom' simultaneously.)
|
||||
|
||||
##### Function No. 1
|
||||
It should be investigated how numbers behave under repeated calculation of the 'square digit sum' (sum of squares of digits). Therefore, one should write and test a function that calculates this 'square digit sum'.
|
||||
We want to investigate how numbers behave under repeated calculation of the 'square digit sum' (sum of squares of digits). Therefore, write and test a function that calculates this 'square digit sum'.
|
||||
|
||||
|
||||
:::{.callout-caution icon="false" collapse="true" .titlenormal}
|
||||
|
||||
## `q2sum(n)` calculates the sum of the squares of the digits of n in the decimal system:
|
||||
## `q2sum(n)` calculates the sum of the squares of the digits of n in base 10:
|
||||
|
||||
```{julia}
|
||||
#| output: false
|
||||
function q2sum(n)
|
||||
s = 0 # accumulator for the sum
|
||||
while n > 9 # as long as still multi-digit...
|
||||
while n > 9 # as long as n is still multi-digit...
|
||||
q, r = divrem(n, 10) # calculate integer quotient and remainder of division by 10
|
||||
s += r^2 # add squared digit to accumulator
|
||||
n = q # continue with the number divided by 10
|
||||
@@ -143,7 +143,7 @@ end
|
||||
```
|
||||
|
||||
:::
|
||||
... and let's test it now:
|
||||
Let's test it now:
|
||||
```{julia}
|
||||
for i in [1, 7, 33, 111, 321, 1000, 73201]
|
||||
j = q2sum(i)
|
||||
@@ -160,10 +160,10 @@ for i in 1:14
|
||||
println(n)
|
||||
end
|
||||
```
|
||||
... and we have here hit one of the '89-cycles'.
|
||||
... and we have now hit one of the '89-cycles'.
|
||||
|
||||
|
||||
#### Function No. 2
|
||||
##### Function No. 2
|
||||
|
||||
We need to test whether repeated application of the function `q2sum()` finally leads to **1** or ends in this **89**-cycle.
|
||||
|
||||
@@ -174,11 +174,11 @@ We need to test whether repeated application of the function `q2sum()` finally l
|
||||
```{julia}
|
||||
#| output: false
|
||||
function q2test(n)
|
||||
while n !=1 && n != 89 # as long as we have not reached 1 or 89....
|
||||
while n !=1 && n != 89 # as long as we have not reached 1 or 89...
|
||||
n = q2sum(n) # repeat
|
||||
end
|
||||
if n==1 return false end # not an 89-number
|
||||
return true # 89-number found
|
||||
if n == 1 return false end # not an 89 cycle
|
||||
return true # 89 cycle found
|
||||
end
|
||||
```
|
||||
|
||||
@@ -188,7 +188,7 @@ end
|
||||
|
||||
```{julia}
|
||||
c = 0 # once again an accumulator
|
||||
for i = 1 : 10_000_000 - 1 # this is how you can separate thousands for better readability in Julia
|
||||
for i = 1 : 10_000_000 - 1 # this is how you can separate thousands for better readability
|
||||
if q2test(i) # q2test() returns a boolean, no further test needed
|
||||
c += 1 # 'if x == true' is redundant, 'if x' is perfectly sufficient
|
||||
end
|
||||
@@ -201,7 +201,7 @@ Numbers for which the iterative square digit sum calculation ends at 1 are calle
|
||||
Here is the complete program again:
|
||||
|
||||
:::{.callout-caution icon="false" collapse="true" .titlenormal}
|
||||
## our solution to Project Euler No 92:
|
||||
## Our solution to Project Euler No 92:
|
||||
|
||||
```{julia}
|
||||
function q2sum(n)
|
||||
|
||||
Reference in New Issue
Block a user