# Learning to Code: Week 7 – Project Euler

**6/23/16**

- No internet at Panera today so I worked on other things for 30m or so.
- Interesting discussion since I was curious about memory allocation in C: http://stackoverflow.com/questions/1963780/when-should-i-use-malloc-in-c-and-when-dont-i
- Went to this meetup at Greater Sum: http://www.meetup.com/greater-sum-atlanta/events/231599132/

Using our collaborative approach to learning, you’ll be introduced to Javascript, the programming language of the internet! This is a great opportunity to experience a collaborative, hands-on approach to learning.

By the end of the evening you’ll understand the basics of Javascript, have written a bunch of Javascript yourself and be equipped with resources to continue learning beyond our workshop.

- The workshop ended up being horrible and I learned nothing new. Here is example of something we worked through arbitrarily in a group: https://github.com/greatersum/intro-to-programming-using-javascript-koans/blob/master/koans/AboutNumbers.js
- We’d just change “FILL_ME_IN” to make the code work, but I didn’t see the point at all. This was supposed to be an introduction class, but nothing was explained. Just two hours of math/coding problems. If you were unfamiliar with coding at all your head would’ve exploded. No basics were covered and we never actually wrote a single line of JS. Just a lot of 1 + 1 = ?
- You can see the actually exercises they used here: https://github.com/greatersum/intro-to-programming-using-javascript-koans/blob/master/koans/AboutNumbers.js
- I looked up Javascript Koans and they seem to be a common thing, but I personally didn’t see how it served as an introduction to JS unless you were already really proficient in another language. But if you were good in another language you wouldn’t need to do these Koan exercises anyway!

**6/24/16**

Shawn linked me this article on Twitter.

I found this part really interesting:

Call your shot. Before you run code, predict out loud exactly what will happen.

**6/25/16**

- Went to this meetup today for Code Newbie: http://www.meetup.com/CodeNewbie-Atlanta/events/229361891/
- The guy that runs this startup Cypress.io talked to our small group about debugging and I learned quite a bit although a lot of it was way over my head.
- He showed us how to use some Dev Tools in chrome to debug a simple Javascript snippet.
- I think he also runs a Javascript group meetup in Atlanta.
- https://www.linkedin.com/in/brian-mann-95463668

- I also learned of a couple of new Slack groups too.
- 404Tech slack group.
- CodeNewbie slack group.

- Started working on Daily Programmer Challenge #269 [Easy] BASIC Formatting.
- Worked for a while trying to figure out how to get input from a .txt file and put it into an array so I can actually manipulate it.
- http://www.programiz.com/c-programming/string-handling-functions
- https://www.daniweb.com/programming/software-development/threads/328416/reading-a-text-file-into-a-string-in-c

- C Programming Tutorial # 42 – fgets() – Reading Strings From A File – Part 1 [HD]

**6/26/16**

- Worked a little more on the BASIC FORMATTING challenge.
- Decided to scrap trying to read the input as a text file as C doesn’t make it very easy to do it as a file with line breaks and I kept getting Segmentation Faults (but I could get it to read the file and print it out, THEN it would break).
- I just put the input string directly into the problem itself now and will work from there. I am not sure how to get the first characters up to the letter V to be deleted and then work from there…
- This is as far as I got. It can print out the input properly but then it segmentation faults.
- https://gist.github.com/anonymous/25d1ed63c5d0875330737bd24899c6d3

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 |
#include <stdio.h> #include <cs50.h> #include <string.h> #include <stdlib.h> #include <ctype.h> //provided input char* input = "12\n····\nVAR I\n·FOR I=1 TO 31\n»»»»IF !(I MOD 3) THEN\n··PRINT \"FIZZ\"\n··»»ENDIF\n»»»»····IF !(I MOD 5) THEN\n»»»»··PRINT \"BUZZ\"\n··»»»»»»ENDIF\n»»»»IF (I MOD 3) && (I MOD 5) THEN\n······PRINT \"FIZZBUZZ\"\n··»»ENDIF\n»»»»·NEXT"; int main(void) { printf("INPUT:\n\n%s\n\n", input); int i; //int j; //int tabcounter = 0; int outputcounter = 0; char* output[250]; //loop through entire string once to remove all the dots and arrows for (i=0; i < strlen(input); i++) { //begin at first letter if (isalpha(input[i]) == 0) { //ignore all · and » and put into a new string output if (input[i] != ("·" || "»")) { output[outputcounter] = &input[i]; printf("%s\n", output[outputcounter]); outputcounter = outputcounter + 1; } } } } |

**6/27/16**

- Only got a chance to read more of Grokking Algorithms chapter 7.
- Dijkstra’s algorithm in python and how to do negative weighted graphs using the Bellman-Ford algorithm.

**6/28/16**

**First day of experimenting with doing coding work first thing in the morning before work. 😴**

CS50 Section Videos:

- Dynamic Memory Allocation
- Hard to wrap my head around this concept. How do people keep track of this?
- Memory must be freed at the end.
- Never free memory twice.

- Structures
- This is really cool.
- They need a semicolon at the end
- Is this similar to linked lists in the Python examples I’ve seen?

- Defining Custom Types
- aka Typedef
- Can rename types like instead of char* call it string to make it easier.
- You can also define structs INSIDE of typedef!

- Recursion
- Another video again. This one talked about the Base Case and Recursive case finally. I found that out in the Algorithm book and knowing that you need a base case to stop a recursive call really helped visualize how to use recursive functions.
- The Collatz Conjecture is talked about. The code I came up with wasn’t as elegant as theirs. Didn’t think of not using a step counter!)

- Call Stack
- Frame for the most recently called stack is always on the top.
- When a new function is called it is
**pushed**onto the stack. - The functions below the top one are paused.
- Once a function is finished it is
**popped**off the stack. - It reminds me a lot of the cup pyramid speed people that take a stack of cups and build them up and then collapse them.

- At work I decided to dive into some Project Euler problems and just do them by hand written out. I will code them up later today if I get a chance.

**Project Euler**

- Problem 1: Multiples of 3 and 5:
- Problem 2: Even Fibonacci Numbers:
- Problem 3: Largest Prime Factor:
- https://www.dropbox.com/s/5hgvx2ywxs382n6/Project%20Euler%203.pdf?dl=0
- http://cboard.cprogramming.com/c-programming/78170-calculating-next-prime-number.html
- Apparently there is a proof that simplifies the problem. At first I was going to write a function that will find the next prime number, but that doesn’t seem to actually be necessary.
- See: https://programmingpraxis.com/2011/09/20/project-euler-problem-3/

Project Euler Problem 3 asks for the largest prime factor of the number 600851475143. We consider first a function to find all the prime factors of any number. A simple way, not necessarily the best way, but sufficient for this task and for many others, works in two steps: first remove factors of 2 while the number is even,then perform trial division by the odd numbers starting from 3 until the factorization is complete.

- Just dividing by odds numbers is a lot easier than calling a function to find the next prime number and using that. Not sure if it’s faster though…

**6/29/16**

**CS50 Problem Set 4 Videos**

- File I/O
- Reading and writing to files.
- http://www.cprogramming.com/tutorial/cfileio.html

- Valgrind
- Program to check for memory leaks and errors.

- Have been contemplating getting a coding coach/tutor/mentor person. https://www.codementor.io/ looks like an interesting site to try. Maybe someone to talk to live once a week and get feedback on code I’ve written in terms of better practices/techniques.

**Problem Set 4**

- BMP, GIF, JPEG, and PNG file formats.
- How many different colors does each format support?
- Which of the formats supports animation?
- What’s the difference between lossy and lossless compression?
- Which of these formats is lossy-compressed?

- Article: http://cdn.cs50.net/2015/fall/psets/4/garfinkel.pdf
- What happens, technically speaking, when a file is deleted on a FAT file system?
- What can someone like you do to ensure (with high probability) that files you delete cannot be recovered?

- Whodunit

- Worked on coding up problem 1 and 2. For problem 1 I wasn’t getting the expected answer and this was my code.

1 2 |
if (i % 3 == 0 || i % 5 == 0) sum = sum + 1; |

- I ran the debugger and it instantly became clear why. (1 should be i, duh). After that it still wasn’t correct and I realized I didn’t quite read the problem correctly. It should only calculate values
**BELOW**1000 not including it. - Also
**sum = sum + i**can be rewritten as**sum += i**. - Click here for final code for problem 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <math.h> /* If we list all the natural numbers below 10 that are multiples of 3 or 5, * we get 3, 5, 6 and 9. The sum of these multiples is 23. * * Find the sum of all the multiples of 3 or 5 below 1000. */ int main(void) { int sum = 0; for (int i=1; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) sum += i; //how do I write this shorter again? } printf("Final sum = %i\n", sum); } |

- Solution = 233168
- Learned about the conditional operator or ternary operator in C. I saw it on Stack Overflow and it made no sense so I looked up the answer on…Stack Overflow of course.
- http://stackoverflow.com/questions/795286/what-does-do-in-c
*This is commonly referred to as the conditional operator, and when used like this:**condition ? result_if_true : result_if_false*

*… if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false.*- This is pretty cool and much shorter to write!

- Ran into a couple of issues coding this one up. First I was thinking about it incorrectly to start with. It was fibonacci numbers up to 4 million not up to the
**4 millionth**fibonacci number (which would be massive). - After I fixed that I ran into another issue where my program should’ve worked, but just hung completely.
- I ran the debugger and everything seemed to sum okay when rolling through a few iterations of the loop.
- I thought that maybe my data types weren’t big enough and changed my ints to long longs and tried again, but it still hangs (that wasn’t the issue at all).
- It works for 10 steps through the loop.
- At 100 steps the CPU on the IDE goes to max and just hangs.
- Not sure what I can do here at all.

- Using this calculator the numbers cross 4mil after the 34th one. So I set my loop to only check up to that number and it worked!
*How would I overcome my program hanging in the future though? Need something with more memory or just need more time to wait? Could it be because my function was recursive and the stack just got super full?*- Click here for final code for Problem 2
- Solution = 4613732

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 |
#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <math.h> /* Even Fibonacci Numbers Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. */ //prototype int fib(int x); int main(void) { int n = 4000000; int sum = 0; for(int i = 1; i < 34; i++) { if (fib(i) <= n) { if (fib(i) % 2 == 0) sum += fib(i); } } printf("Final sum = %i\n", sum); } // returns next fibonacci number int fib(int x) { if (x == 0) return 0; else if (x == 1) return 1; else return (fib(x-1) + fib(x-2)); } |

- I just came to the realization that attempting to write a function that can calculate the next prime number from any given number would be…well to say the least “really complex.” I don’t know a way to write it that wouldn’t use straight up brute force, but doing a little googling I do see there are algorithms that do it faster.
- https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (the animation here is pretty sweet)
- Most likely for this problem I will stick the to the “divide by odds” strategy and not mess with that higher level math problem solving. Maybe someone wrote a library that does this already?
- I did find this solution to finding the next prime number which makes perfect sense, but is brute force checking. http://stackoverflow.com/questions/27303306/finding-next-prime-number-of-x-in-c
- I may try to use it and see what happens.