maandag 10 oktober 2011

Could not load file or assembly or one of its dependencies

Today i copied a .NET project from my computer at work to my computer at home.
When i build the project at home i was facing a weird problem.  I got a run time error:

"Could not load file or assembly or one of its dependencies"


This message drives me nuts. The problem was the Copy Local attribute of a referenced assembly file.
As you can see The Copy Local attribute is set to True in the property window of the reference assembly.

When you  take a look in the project file (csproj) you see that  <Private> is set True. This indicates that the assembly must be copied to the projects output directory.


    <Reference  Include="...>
<HintPath>..\Heihoeve.Module\bin\Debug\Heihoeve .Module.dll</HintPath>

      <Private>True</Private>
    </Reference>

But this True is not really True! The assembly was not copied at all to the Local output directory! That is why the system couldn't find the assembly file and gives the error message above.

The simplicity of the solution is inversely proportional to its maddening level:

1) Set the Copy Local attribute to False and build the project.
2) Set the Copy Local attribute to True and rebuild again.

Now the assembly is copied to the project output directory and the project runs successfull!




woensdag 31 augustus 2011

Remove password protection in Word 2007

Today i had to deal with a document, written in Word 2007, which was protected by a password.
I could open the document but most menu functions where grayed out and i could not select any text.

Because i wanted to edit this document i played around and finally found a way to remove the password.


The following steps removed the protection from the document:


  1. Open protected document in Word 2007.
  2. Click the colored Word button in the upper left corner and Save document as Word 97-2003 document.
  3. Close Word and re-open the just created document.doc (note file extension is doc now!)
  4. Choose menu Review->Protect Document and press button Stop Protection .



No password is asked and the document becomes unprotected now! 



vrijdag 1 juli 2011

Euler problem 61


Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

TriangleP3,n=n(n+1)/21, 3, 6, 10, 15, ...
SquareP4,n=n21, 4, 9, 16, 25, ...
PentagonalP5,n=n(3n−1)/21, 5, 12, 22, 35, ...
HexagonalP6,n=n(2n−1)1, 6, 15, 28, 45, ...
HeptagonalP7,n=n(5n−3)/21, 7, 18, 34, 55, ...
OctagonalP8,n=n(3n−2)1, 8, 21, 40, 65, ...

The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
  1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
  2. Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
  3. This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.

analyse:

Start with n = 19. This is the minimal number to generate a four digit Octagonal number.



Problem 61 =       28684 elapsed time:    1 ms. Test Passed.

Euler problem 60


The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

















Problem 60 =       26033 elapsed time:  236 ms. Test Passed.

vrijdag 1 april 2011

Euler problem 59


Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.
Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher1.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.



Problem 59 =      107359 elapsed time:   61 ms. Test Passed.

Euler problem 58


Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.
37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18  5  4  3 12 29
40 19  6  1  2 11 28
41 20  7  8  9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49
It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.
If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?



Problem 58 =       26241 elapsed time:  122 ms. Test Passed.

Euler problem 57


It is possible to show that the square root of two can be expressed as an infinite continued fraction.
√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
By expanding this for the first four iterations, we get:
1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...
The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

analyse:
If you examine the function: Log10(Numerator)/Log10(Denominator) 
you'll notice a pattern. It looks like that each 8th and 13th iteration 
yield to a fraction 2 (iteration 8,13,21,26,34,39,...)
So in 1000 iterations there are 2 positive interations every 13 iterations. 
=> answer: 2 * 1000 / 13 









Problem 57 =         153 elapsed time:    0 ms. Test Passed.

Euler problem 56


A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?

Solution: 


Problem56 =         972 elapsed time:   17 ms. Test Passed.

Euler problem 55


If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
Not all numbers produce palindromes so quickly. For example,
349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
That is, 349 took three iterations to arrive at a palindrome.
Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
How many Lychrel numbers are there below ten-thousand?
NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.

analyse:

performance improvements:

1) It turned out empirically that 24 loops yield also to the correct answer. 21 -> 15 ms.
2) Without Palindrome function execution is two times faster. 15 -> 7 ms.

Problem 55 = 249 elapsed time: 7 ms. Test Passed.

Euler problem 54


In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following five hands dealt to two players:
HandPlayer 1Player 2Winner
15H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
25D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
32D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
44D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
52H 2D 4C 4D 4S
Full House
With Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1
The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?



Problem 54 =         376 elapsed time:    6 ms. Test Passed.

Euler problem 53


There are exactly ten ways of selecting three from five, 12345:
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
In combinatorics, we use the notation, 5C3 = 10.
In general,
nCr =
n!
r!(n−r)!
,where r ≤ nn! = n×(n−1)×...×3×2×1, and 0! = 1.
It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
How many, not necessarily distinct, values of  nCr, for 1 ≤ n ≤ 100, are greater than one-million?


 performance improvements:

 1) add break:               12374 us -> 880 us
 2) let minimun n=23, r=4: 880 us -> 277 uS



Problem 53 =        4075 elapsed time:    0 ms. Test Passed.



Euler problem 52


It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.


analyse:

Lets say we have a number n. According the problem description the number 6*n must have the same number of digits.
this is only the case when the number n starts with digit 1. i.e. n = 1000 6n = 6000 (both four digits) n = 2000 6n = 12000. n and 6n have resp. 4 and 5 digits so they can never have the same digits.
more precisely, n must be between 1000 and 1666 (=9999/6)

performance improvements:

Use bitmap to store digits. 50 -> 8 ms.



Problem 52 =      142857 elapsed time:    8 ms. Test Passed.


Euler problem 51

By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

analyse:
1) If we replace the last digit with 0-9 we get: xxxx0, xxxx1, xxxx2, ..., xxxx9. 
    This yield at least to four even numbers, xxxx2,xxxx4,xxxx6,xxxx8, which of cause aren't prime. 
   So the last digit can't be replaced.
2) Digit must be less or equal than 2 to generate a family of eight.  0-7, 1-8, 2-9.


performance improvements: 30 ms -> 17 ms.
add 1) Don't check last digit for equalness.
add 2) Only check digits less or equal 2.






Problem51 =      121313 elapsed time:   17 ms. Test Passed.

Euler problem 50

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?

analyse:


1) Sum all primes below one million.
2) Substract primes from sum until a new prime is found.


Problem 50 =      997651 elapsed time:    0.007 ms. Test Passed.

vrijdag 18 maart 2011

Euler problem 49

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
What 12-digit number do you form by concatenating the three terms in this sequence?

analyse:
start with next odd number 1489 and increment by two.



Problem49 =296962999629 elapsed time:    2 ms. Test Passed.

Euler problem 48

The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

analyse:
Because only the last ten digits are needed in the answer we can avoid big integers by 
truncating each calculation to ten digits.



Problem48 =  9110846700 elapsed time:   18 ms. Test Passed.

donderdag 17 maart 2011

Euler problem 47

The first two consecutive numbers to have two distinct prime factors are:
14 = 2 × 7
15 = 3 × 5
The first three consecutive numbers to have three distinct prime factors are:
644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.
Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?

Performance improvements:
Suppose we have to check number 80 for prime divisibility. The first prime can only be 2 because the next prime 3 will yield to a greater number (3 * 3 * 3 * 3 > 80). 
Stop prime division when maximum prime number is reached yield to 20 x faster code execution. The maximum prime depends on primes already found.


number  = x * x * x * x  ( x = maximum prime number )
number  = 2 * x * x * x  
number  = 2 * 3 * x * x  
number  = 2 * 3 * 5 * x  





Problem47 =      134043 elapsed time:  117 ms. Test Passed.

Euler problem 46

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12
It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

Performance improvement:
Add .Tolist() extension result in 2 times faster execution.

List<int> primes = new PrimeNumberSieve().ToList(); // 15 ms -> 8 ms.




Problem46 =        5777 elapsed time:   8 ms. Test Passed.