-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Project Euler Problem 22 | ||
I had to approach this by modifying the factors function from p0003, but it | ||
seemed to work fairly well. | ||
Problem: | ||
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file | ||
containing over five-thousand first names, begin by sorting it into | ||
alphabetical order. Then working out the alphabetical value for each name, | ||
multiply this value by its alphabetical position in the list to obtain a name | ||
score. | ||
For example, when the list is sorted into alphabetical order, COLIN, which is | ||
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would | ||
obtain a score of 938 × 53 = 49714. | ||
What is the total of all the name scores in the file? | ||
*/ | ||
using System; | ||
|
||
namespace Euler | ||
{ | ||
public class p0022 : IEuler | ||
{ | ||
public object Answer() | ||
{ | ||
int answer = 0; | ||
string[] names = Utilities.getDataFileText("p0022_names.txt").Replace("\"", "").Split(','); | ||
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (2, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (3, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (5, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (7, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (8, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (9, ubuntu-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, macos-latest)
Check failure on line 30 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, macos-13)
|
||
names.Sort(); | ||
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (2, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (3, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (5, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (7, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (8, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (9, ubuntu-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, macos-latest)
Check failure on line 31 in csharp/Euler/p0022.cs GitHub Actions / csharp (6, macos-13)
|
||
for (int i = 0; i < names.Length; i += 1) | ||
{ | ||
int sum = 0; | ||
foreach (char c in names[i]) | ||
{ | ||
sum += c & 0x3F; | ||
} | ||
answer += sum * (i + 1); | ||
} | ||
return answer; | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
C# Implementation of Problem 22 | ||
=============================== | ||
|
||
View source code :source:`csharp/Euler/p0022.cs` | ||
|
||
Includes | ||
-------- | ||
|
||
- `utils <./utils.html>`_ | ||
|
||
Problem Solution | ||
---------------- | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: p0022 | ||
.. csharp:inherits:: Euler.IEuler | ||
.. csharp:method:: object Answer() | ||
.. literalinclude:: ../../csharp/Euler/p0022.cs | ||
:language: csharp | ||
:linenos: |