Awots
Awots

Reputation: 75

Q: F#: Split String to Array [simple]

Yello, Here I am, friday noon, trying to break one of the Euler problems.

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629 91942213363574161572522430563301811072406154908250 23067588207539346171171980310421047513778063246676 and so on..

I decided to put all lines of numbers in a string.

let numString = 
"37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250"

Next step (instead of creating an int array then alt+shift add quotations and semicolons) is to split all lines to an int array. (later referred to as intArray)

So this is where i have some problems. How does one simply split the string array to an integer array?

Lastly I want to sum up the first 10 rows

let sum = intArray |> Seq.map bigint.Parse |> Seq.take(10) |> Seq.sum

Upvotes: 0

Views: 786

Answers (2)

Ringil
Ringil

Reputation: 6537

Well, you have to split your string to get the numbers of each of the lines. Then if what you want is to change the array to a character array there is a built in function to do so:

let charArray = splitString.ToCharArray()

You can then convert each character to an integer with something like this:

let intArray = charArray |> Array.map( fun i -> int(System.Char.GetNumericValue(i)))

I get:

val intArray : int [] =
  [|3; 7; 1; 0; 7; 2; 8; 7; 5; 3; 3; 9; 0; 2; 1; 0; 2; 7; 9; 8; 7; 9; 7; 9; 9;
    8; 2; 2; 0; 8; 3; 7; 5; 9; 0; 2; 4; 6; 5; 1; 0; 1; 3; 5; 7; 4; 0; 2; 5; 0|]

Upvotes: 3

s952163
s952163

Reputation: 6324

That's Project Euler problem 13. I think the point of those problems is that you try to solve it on your own...

A string is nothing but a character array...can you go from here?

"828282" |> Seq.map char
val it : seq<char> = seq ['8'; '2'; '8'; '2'; ...]

It's unclear from your question if you want to solve the original problem or a new one (first 10 rows). Good luck.

Upvotes: 2

Related Questions