Skip to main content
Code Review

Return to Question

Some extra explanation on the asFarAsPossibleCondition function
Source Link
Ben
  • 283
  • 1
  • 8

To start from the next prime, you simply drop the first prime, and so forth, e.g.

last $ fromJust $ asFarAsPossibleCondition isPrime $ scanl1(+) $ take 20 $ drop 1 primes
499

This is the highest consecutive prime sum that results in a prime number that you can get with x<=20 prime numbers starting from 3.

To start from the next prime, you simply drop the first prime, and so forth.

To start from the next prime, you simply drop the first prime, and so forth, e.g.

last $ fromJust $ asFarAsPossibleCondition isPrime $ scanl1(+) $ take 20 $ drop 1 primes
499

This is the highest consecutive prime sum that results in a prime number that you can get with x<=20 prime numbers starting from 3.

added 467 characters in body
Source Link
Ben
  • 283
  • 1
  • 8

asFarAsPossibleCondition looks for the last element in a list that satisfies a condition then returns that list up to that point (e.g. asFarAsPossibleCondition (<5) [1..10] -> Just [1,2,3,4]. (Note that it differs from takeWhile, since takeWhile will start at the beginning of the list and stops as soon as it encounters an element that does not satisfy the condition. My function starts at the end of the list and looks for the last element that satisfies the condition, regardless of whether there are elements before that that don't satisfy the condition, e.g. asFarAsPossibleCondition (<5) [1,2,3,4,5,6,7,8,9,10,4] -> Just [1,2,3,4,5,6,7,8,9,10,4]. This is useful to check a list of consecutive prime sums (e.g. [2, 5, 10, ...]) and get the longest one that adds to a prime, for instance:

asFarAsPossibleCondition looks for the last element in a list that satisfies a condition then returns that list up to that point (e.g. asFarAsPossibleCondition (<5) [1..10] -> Just [1,2,3,4]. This is useful to check a list of consecutive prime sums (e.g. [2, 5, 10, ...]) and get the longest one that adds to a prime, for instance:

asFarAsPossibleCondition looks for the last element in a list that satisfies a condition then returns that list up to that point (e.g. asFarAsPossibleCondition (<5) [1..10] -> Just [1,2,3,4]. (Note that it differs from takeWhile, since takeWhile will start at the beginning of the list and stops as soon as it encounters an element that does not satisfy the condition. My function starts at the end of the list and looks for the last element that satisfies the condition, regardless of whether there are elements before that that don't satisfy the condition, e.g. asFarAsPossibleCondition (<5) [1,2,3,4,5,6,7,8,9,10,4] -> Just [1,2,3,4,5,6,7,8,9,10,4]. This is useful to check a list of consecutive prime sums (e.g. [2, 5, 10, ...]) and get the longest one that adds to a prime, for instance:

Post Reopened by Community Bot, Malachi, Dan Oberlam, nhgrif, rolfl
Edited this to comply with SE rules
Source Link
Ben
  • 283
  • 1
  • 8

My solution, which does not work, is(very fast):

asFarAsPossibleCondition :: (a -> Bool) -> [a] -> Maybe [a]
asFarAsPossibleCondition _ [] = Nothing
asFarAsPossibleCondition f xs = if f $ last xs then Just xs else asFarAsPossibleCondition f $ filterinit isPrimexs
maxPrimeSum :: Int -> Int
maxPrimeSum n = snd $ takeWhilemaximumBy (<1000000comparing fst) $series
 scanl1 where
 series = map (+\x -> (length x, last x)) primes$ mapMaybe checkSeries [0..10]

I don't really understand why. Going through it step by step:

 checkSeries x = asFarAsPossibleCondition (isPrime) $ takeWhile (<n) $ scanl1 (+) $ drop x primes

generates an infiniteasFarAsPossibleCondition looks for the last element in a list that satisfies a condition then returns that list up to that point (e.g. asFarAsPossibleCondition (<5) [1..10] -> Just [1,2,3,4]. This is useful to check a list of consecutive prime sums, i. (e.g. [2,5,10,17,28,41,58,77,100,129,...]) and get the longest one that adds to a prime, for instance:

takeWhilelast $ fromJust $ asFarAsPossibleCondition isPrime $ scanl1(<1000000+) $ take 20 primes

takes all the elements of this list below 1,000,000

filter281 isPrime

gets rid ofThis is the non-prime numbershighest consecutive prime sum that results in a prime number that list you can get with x<=20 prime numbers starting from 2 (2+たす3+たす5+たす7+たす11+たす13+たす17+たす19+たす23+たす29+たす31+たす37+たす41+たす43 = 281).

last

gives meTo start from the last/largest element innext prime, you simply drop the listfirst prime, and so forth.

It worksThe rest is fairly obvious; it checks for each starting prime the example < 100greatest prime below 1, but000,000, gets the length and the prime it failsadds up to, then gets the example < 1000 (it gives me 287)prime with the maximum length.

Could anyone tell me what I'm doing wrong and how I could improve my solution?Any advice is welcome, specifically on:

  • the use of Maybe (I'm just learning about this); is it correct? Justified? ...
  • how to determine on how many items that I should map the checkSeries function on. ([0..10] was sufficient, but that was simply because I got lucky).
  • is there a builtin that does what my asFarAsPossibleCondition function does?

My solution, which does not work, is:

last $ filter isPrime $ takeWhile (<1000000) $ scanl1 (+) primes

I don't really understand why. Going through it step by step:

scanl1 (+) primes

generates an infinite list of prime sums, i.e. [2,5,10,17,28,41,58,77,100,129,...]

takeWhile (<1000000)

takes all the elements of this list below 1,000,000

filter isPrime

gets rid of the non-prime numbers in that list

last

gives me the last/largest element in the list.

It works for the example < 100, but it fails the example < 1000 (it gives me 287).

Could anyone tell me what I'm doing wrong and how I could improve my solution?

My solution (very fast):

asFarAsPossibleCondition :: (a -> Bool) -> [a] -> Maybe [a]
asFarAsPossibleCondition _ [] = Nothing
asFarAsPossibleCondition f xs = if f $ last xs then Just xs else asFarAsPossibleCondition f $ init xs
maxPrimeSum :: Int -> Int
maxPrimeSum n = snd $ maximumBy (comparing fst) series
 where
 series = map (\x -> (length x, last x)) $ mapMaybe checkSeries [0..10]
 checkSeries x = asFarAsPossibleCondition (isPrime) $ takeWhile (<n) $ scanl1 (+) $ drop x primes

asFarAsPossibleCondition looks for the last element in a list that satisfies a condition then returns that list up to that point (e.g. asFarAsPossibleCondition (<5) [1..10] -> Just [1,2,3,4]. This is useful to check a list of consecutive prime sums (e.g. [2,5,10,...]) and get the longest one that adds to a prime, for instance:

last $ fromJust $ asFarAsPossibleCondition isPrime $ scanl1(+) $ take 20 primes
281 

This is the highest consecutive prime sum that results in a prime number that you can get with x<=20 prime numbers starting from 2 (2+たす3+たす5+たす7+たす11+たす13+たす17+たす19+たす23+たす29+たす31+たす37+たす41+たす43 = 281).

To start from the next prime, you simply drop the first prime, and so forth.

The rest is fairly obvious; it checks for each starting prime the greatest prime below 1,000,000, gets the length and the prime it adds up to, then gets the prime with the maximum length.

Any advice is welcome, specifically on:

  • the use of Maybe (I'm just learning about this); is it correct? Justified? ...
  • how to determine on how many items that I should map the checkSeries function on. ([0..10] was sufficient, but that was simply because I got lucky).
  • is there a builtin that does what my asFarAsPossibleCondition function does?
Post Closed as "Not suitable for this site" by 200_success
Source Link
Ben
  • 283
  • 1
  • 8
Loading
lang-hs

AltStyle によって変換されたページ (->オリジナル) /