Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
we are doing the project euler challenges with Haskell. Currently we're at no 7 "find the 10001st prime number"
Now we have a simple filter algorithm, nothing fancy but the calculation time varies widely with how you call the function.
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
when using ghci with primes and just stopping when you see the numbers go over 100000 or so it usually runs ~3 seconds or so
when using primes !! 10001 we have significantly longer runtimes up to 20 or 25 seconds. Once the list is filled, you can call the 10001st element instantly both ways.
Does anyone have a good explanation on how this works or why it takes so long in the second method?
local problem, needs own troubleshooting
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
I'm new to rust and tried writing a function that adds all digits at the uneven positions together (For context: I'm trying to solve this Daily programmer #370. But somehow my results are off by a bit and I can't wrap my head around what#s going on.
number.to_string().chars()
.enumerate()
.filter(|(i, _)| i % 2 != 0)
.map(|(_, c)| c as u64 - 48)
.sum::<u64>()
Now if I input 4210000526 I get 13 as a result, although I should get 15. For some reason this only happens with a filter for uneven positions, "i % 2 == 0" works perfectly fine.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Python:
I have researched a lot into this, but as a beginner I don't understand the solutions, let alone apply them to my simple problem:
def min_max(xs):
xs = []
for i in xs:
y = (min, max)
return y
"local variable 'y' referenced before assignment"
I don't understand why this does NOT work!!
I've defined Y in the line before return, i know there is another way to do it, but i really need to know why this method is wrong!
Thanks!
assuming that min and max are defined somewhere else....
The loop for i in xs will go around once for each item in xs. Since xs is empty, the loop will not run at all. So, y is never set.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
main = print $ smallestFactor 15
factors n = [x | x <- [2..n], n `mod` x == 0]
smallestFactor n = min[factors n]
I understand that it's a pretty trivial question but someone told me that I could use min get grab out the smallest value of a element list. Though, I'm not exactly sure how one is suppose to use it.
You mean minimum :: Ord a => [a] -> a. Plus, you're erroneously using square brackets when you should be using parentheses. Don't use square brackets when you're not explicity defining a list!
Here's the corrected part:
smallestFactor n = minimum (factors n)
The rest of your code is fine.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I type this into my texteditor:
boombang xs = [ if x < 10 then ”BOOM!” else ”BANG!”
But when trying to load it, my terminal denies it and and says:
Probably some dumb rookie mistake, but I can't seem to find the solution...:/
There are two problems here.
The first is, you're not using plain ASCII quotation marks (U+0022) ". You're using right quotation marks (U+201D) ”. There's probably a keyboard setting you're using that's causing this, and using certain text editing programs can also prevent this.
Secondly, you seem to be either missing a right bracket. As pointed out, this is probably meant to be part of a list comprehension. You need to write all of the list comprehension for it to work.
The corrected code is:
boombang xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am learning Haskell, studying for a midterm. there is a test quiz I'm looking at at the moment with questions on type inference. some of the questions contain forward slashes and I have no idea what they represent. the goal is to determine the type of the function by looking at its variables. here are some examples of the questions im supposed to find the type of:
f1 x = (/x)
f3 x = (x/)
f5 = (1/)
f6 = (/2)
I have googled with no luck, what does the forward slash mean?
These are sections: the expression (/x) is equivalent to \y -> y / x and (x/) is equivalent to \y -> x / y.