Skip to content

Commit 2066940

Browse files
committed
1st and 2nd challenge
1 parent 5308140 commit 2066940

File tree

8 files changed

+204
-0
lines changed

8 files changed

+204
-0
lines changed

1-evenFibonacci/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Hello everybody! Welcome to the first #code4bytes challenge! Because this is our first challenge, I'd like to keep things quite easy! So here is the challenge:
2+
3+
```
4+
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:
5+
6+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
7+
8+
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
9+
```
10+
11+
As you can see this is easily bruteforcable, but try to come up with a solution that doesn't compute every number in the fibonacci sequence. Hint: try to analyze the sequence to see if you can skip certain calculations.
12+
13+
If you're done, DM your solution along with the code and a screenshot of your terminal/cmd as stated by @EmotionIce. If we feel as though you might not have written it yourself we might as some questions about it to see if you really came up with it yourself! So dont cheat! Good luck and happy coding!
14+
15+
- Challenge from projecteuler.net

1-evenFibonacci/submission1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Written by manish#9999
2+
# 29/01/2020
3+
4+
a = [2, 8]
5+
6+
total = 10
7+
8+
while True:
9+
next = a[-2] + 4*a[-1]
10+
if next > 4*10**6: break
11+
a += [next]
12+
total += next
13+
14+
print(total)
15+

1-evenFibonacci/submission2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Written by frog#7088
2+
# 01/29/2020
3+
4+
5+
# First implementation
6+
7+
cache = {}
8+
def evenFib(number):
9+
if number in cache:
10+
return(cache[number])
11+
else:
12+
if number == 0:
13+
return(0)
14+
elif number == 1:
15+
return(2)
16+
else:
17+
cacheAdd = (4 * evenFib(number-1)) + evenFib(number-2)
18+
cache[number] = cacheAdd
19+
return(cacheAdd)
20+
21+
milEvenFibs = 0
22+
x = 1
23+
while True:
24+
temp = evenFib(x)
25+
x += 1
26+
if temp > 4_000_000:
27+
break
28+
milEvenFibs += temp
29+
print(milEvenFibs)
30+
31+
# Second Implementation
32+
33+
cache = [0,2]
34+
milEvenFibs = 0
35+
cacheAdd = 2
36+
while True:
37+
milEvenFibs += cacheAdd
38+
cacheAdd = (4 * cache[1]) + cache[0]
39+
cache[0] = cache[1]
40+
cache[1] = cacheAdd
41+
if cacheAdd > 4_000_000:
42+
break
43+
print(milEvenFibs)
44+

1-evenFibonacci/submission3.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Written by TrizlyBear#7066
2+
// 01/29/2020
3+
4+
var fib = [1,2]
5+
var c = 1
6+
while(fib[c] + fib[c-1] < 4000000){
7+
var newo = fib[c] + fib[c-1]
8+
var old = fib
9+
fib.push(newo)
10+
11+
// console.log(fib)
12+
if(newo + old[c] > 4000000){
13+
var e = 0
14+
var count = 0
15+
fib.forEach(el => {
16+
if(el % 2 == 0){
17+
e = e + el
18+
}
19+
count++
20+
if(count == fib.length){
21+
console.log(e)
22+
process.exit()
23+
}
24+
})
25+
}
26+
c++;
27+
}

2-pandigitalPrime/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Hello! This is the second #code4bytes challenge!
2+
As you can probably see, it's quite a bit harder, but it should still be do-able.
3+
```
4+
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
5+
6+
What is the largest n-digit pandigital prime that exists?
7+
```
8+
As with the previous challenges, it's possible to brute force it by trying every combination one by one but note that this will take hours if not days, so try to be smart about what you do first and how you do it! To give you a frame of reference, it shouldn't take more than 1 minute MAX of runtime to calculate the final solution. Tip of the day: prime calculations of O(n) are too slow!
9+
10+
Good luck and happy coding!

2-pandigitalPrime/base_solution.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
array = [i for i in reversed(range(1,8))]
2+
numbers = int(''.join(str(e) for e in array))
3+
4+
def isPandigital(n):
5+
for i in array[:len(str(n))]:
6+
if(str(i) not in list(str(n))):
7+
return False
8+
9+
10+
return True
11+
12+
def buildPrimes(n):
13+
primes = [True]*(n+1)
14+
p = 2
15+
while(p*p <= n):
16+
if(primes[p]):
17+
for i in range(p*2, n+1, p):
18+
primes[i] = False
19+
p += 1
20+
21+
return primes
22+
23+
primes = buildPrimes(numbers)
24+
for p in reversed(range(len(primes))):
25+
if(isPandigital(p) == True):
26+
if(primes[p] == True):
27+
print(p)
28+
break

2-pandigitalPrime/submission1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Written by manish#9999
2+
# 30/01/2020
3+
4+
def get_primes(n):
5+
m = n+1
6+
numbers = [True] * m
7+
for i in range(2, int(n**0.5 + 1)):
8+
if numbers[i]:
9+
for j in range(i*i, m, i):
10+
numbers[j] = False
11+
primes = []
12+
for i in range(2, m):
13+
if numbers[i]:
14+
primes.append(i)
15+
return primes
16+
17+
primes = get_primes(9999999)
18+
answer = 0
19+
for each in primes:
20+
if sorted([int(k) for k in str(each)]) == [i+1 for i in range(len(str(each)))]:
21+
answer = each
22+
print(answer)

2-pandigitalPrime/submission2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Written by frog#7088
2+
# 30/01/2020
3+
def pandig(number):
4+
number = str(number)
5+
numbers = {}
6+
for x in number:
7+
if x in numbers:
8+
return(False)
9+
elif int(x) > len(number):
10+
return(False)
11+
numbers[x] = None
12+
return(True)
13+
def isPrime(number):
14+
numberMax = round(number**0.5)+1
15+
for x in range(2, numberMax):
16+
if not number % x:
17+
return False
18+
return True
19+
def genPrimes(maxNumber):
20+
maxPrimeNumber = round(maxNumber**0.5)+1
21+
badIndex = []
22+
forLoopArray = list(range(2,maxPrimeNumber))
23+
print(forLoopArray)
24+
for x in forLoopArray:
25+
if isPrime(x):
26+
print(x)
27+
currentNumber = x
28+
while True:
29+
currentNumber += x
30+
if currentNumber >= maxNumber:
31+
break
32+
if not currentNumber in badIndex:
33+
badIndex.append(currentNumber)
34+
finalList = list(set(range(1,maxNumber+1))-set(badIndex))
35+
finalList.pop(-1)
36+
finalList.pop(0)
37+
return(finalList)
38+
largest = 0
39+
primeList = genPrimes(7654321)
40+
for x in primeList:
41+
if pandig(x):
42+
largest = x
43+
print(largest)

0 commit comments

Comments
 (0)