Skip to content

Instantly share code, notes, and snippets.

@gnp
gnp / fibalt.c
Created July 14, 2010 03:00
Unconventional tiny solution to fib(n) Fibonacci sequence programming problem c. 2005-04-19
#include <math.h>
#include <stdio.h>
unsigned int fibalt(unsigned int n) { return pow(1.618033988749, n) / 2.2360679774 + 0.5; }
/*unsigned int fibalt(unsigned int n) { return pow(1.618033988749, n) / sqrt(5) + 0.5; }*/
unsigned int fib(unsigned int n) {
unsigned int a[2] = { 0, 1 };
unsigned int i = 0;
for (; n > 0; --n) {
@gnp
gnp / japh.sh
Created July 14, 2010 03:25
Random JAPH, including newline. (JAPH = Just Another Perl Hacker) c. 2001-09-10
perl -e 'srand(-2091643526); print chr rand 90 for (0..4)'
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.
----------
$ perl -e '$a = 0; foreach $x (1..999) { if (($x % 3 == 0) || ($x % 5 == 0)) { $a += $x; } } print "$a\n"; '
233168
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.
----------
$perl -e '($a, $b) = (1, 2); while ($b <= 4000000) { $t += $b if $b %2 == 0; ($a, $b) = ($b, $a + $b); } print "$t\n";'
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
----------
$ perl -e '$x = 600851475143; for $i (2..sqrt($x)) { print "$i\n" if $x % $i == 0; }'
71
839
1471
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
----------
$ perl -e 'I: for ($I = 999; $I >= 100; --$I) { for ($J = $I; $J >= 100; --$J) { $p = $I * $J; $q = scalar reverse $p; if ($p eq $q) { printf "%8d = %3d * %3d\n", $p, $I, $J; } } }' | sort -n -r | head -1
906609 = 993 * 913
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
----------
2 = 2
3 = 3
4 = 2 * 2
5 = 5
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
----------
Too easy in Mathematica:
In[2]:= Prime[10001]
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776