Skip to content

Instantly share code, notes, and snippets.

View larryv's full-sized avatar

Lawrence Velázquez larryv

View GitHub Profile
@larryv
larryv / gist:2640482
Created May 8, 2012 23:49
Cool string definition
/*
* String constants (C syntax)
* Escape sequence \c is accepted for all characters c. Except for
* \n \t \b \f, the result is c.
*
*/
\" {
string_buf_ptr = string_buf;
BEGIN(STRING);
@larryv
larryv / gist:2632239
Created May 8, 2012 03:07
Completed COOL lexical analyzer
/*
* The scanner definition for COOL.
*/
/*
* Stuff enclosed in %{ %} in the first section is copied verbatim to the
* output, so headers and global definitions are placed here to be visible
* to the code in the file. Don't remove anything that was here initially
*/
%{
@larryv
larryv / logo.ps
Created April 22, 2012 22:17
MIT logo in PostScript
newpath
300.0 400.0 moveto
gsave
currentpoint translate
-241.66666666666669 0.0 rmoveto
108.33333333333334 0.0 rmoveto
gsave
currentpoint translate
-108.33333333333334 0.0 rmoveto
25.0 0.0 rmoveto
@larryv
larryv / gist:2379742
Created April 13, 2012 20:03
Determine whether a C string consists entirely of digits.
int is_valid_ref(char *arg)
{
do {
if (!isdigit(*arg))
return 0;
} while (*++arg);
return 1;
}
#!/usr/bin/env ruby1.9
# Project Euler, Problem 57
#
# It is possible to show that the square root of two can be expressed as an
# infinite continued fraction.
#
# sqrt(2) = 1 + 1/(2 + 1/(2 + 1/(2 + ...))) = 1.414213...
#
# By expanding this for the first four iterations, we get:
#!/usr/bin/env ruby1.9
# Project Euler, Problem 36
#
# The decimal number, 585 = 1001001001 (binary), is palindromic in both
# bases.
#
# Find the sum of all numbers, less than one million, which are palindromic
# in base 10 and base 2.
#
class Integer
def to_binary_s(n)
(n - 1).downto(0).inject("") {|str, i| str << self[i].to_s}
end
end
-- Project Euler, Problem 48
--
-- ==========
-- The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
--
-- Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
-- ==========
--
-- Lawrence Velazquez
-- 13 June 2011
-- Project Euler, Problem 97
--
-- ==========
-- The first known prime found to exceed one million digits was discovered
-- in 1999, and is a Mersenne prime of the form 2^6972593 - 1; it contains
-- exactly 2,098,960 digits. Subsequently other Mersenne primes, of the
-- form 2^p - 1, have been found which contain more digits.
--
-- However, in 2004 there was found a massive non-Mersenne prime which
-- contains 2,357,207 digits: 28433 * 2^7830457 + 1.
#!/usr/bin/env ruby1.9
# Project Euler, Problem 12
#
# The sequence of triangle numbers is generated by adding the natural
# numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 =
# 28. The first ten terms would be:
#
# 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
#