mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   jvang (https://www.mersenneforum.org/forumdisplay.php?f=153)
-   -   ¿Learning how to learn… (https://www.mersenneforum.org/showthread.php?t=23429)

henryzz 2018-07-19 10:51

I would suggest [url]https://www.codewars.com/kata/take-a-number-and-sum-its-digits-raised-to-the-consecutive-powers-and-dot-dot-dot-eureka/haskell[/url] is a decently challenging puzzle for you.

I discovered the zipWith function while solving this. It is basically an extension to the map function allowing the function to have 2 arguments rather than 1. There is also zipWith3 for 3 arguments etc.

jvang 2018-07-19 14:07

[QUOTE=R. Gerbicz;492086]Just tried this semi-broken Python code:
[CODE]
def is_prime(n):
return (n>1 and (n==2 or pow(2,n,n)==2))
[/CODE]

And surprisingly all 16 tests passed in final. They should learn much more how to setup a good input set.[/QUOTE]

Hmm, I think I replicated that in Haskell but it takes too long to run :sad:

[CODE]module IsPrime where

isPrime :: Integer -> Bool
isPrime 2 = True
isPrime x = if x < 2 then False else if mod (2 ^ x) x == 2 then True else False[/CODE]

[QUOTE=Nick;492102]For untarring, you can just use "tar xvf newstuff.tar", first checking that nothing important is going to be overwritten!
The other extensions are only necessary if you want tar to compress as well as combine files.
The useful thing to remember with programs like tar is to use relative filenames
so that you can unpack a file into a different directory from the one you packed it from.
(I think some versions of tar do this automatically these days.)

When you make notes, I would suggest you read them afterwards critically in order to improve them.
For example, what you have written about recursion is correct, but you could add something about cases with two functions
where the first calls the second and then the second function calls the first, or 3 functions calling each other in a circle, etc.[/QUOTE]

Ok, thanks for the tips!

[QUOTE=henryzz;492107]I would suggest [url]https://www.codewars.com/kata/take-a-number-and-sum-its-digits-raised-to-the-consecutive-powers-and-dot-dot-dot-eureka/haskell[/url] is a decently challenging puzzle for you.

I discovered the zipWith function while solving this. It is basically an extension to the map function allowing the function to have 2 arguments rather than 1. There is also zipWith3 for 3 arguments etc.[/QUOTE]

Hmm, that one looks weird, time to have a go at it...

henryzz 2018-07-19 16:13

[QUOTE=jvang;492120]Hmm, I think I replicated that in Haskell but it takes too long to run :sad:

[CODE]module IsPrime where

isPrime :: Integer -> Bool
isPrime 2 = True
isPrime x = if x < 2 then False else if mod (2 ^ x) x == 2 then True else False[/CODE]

[/QUOTE]

Why does that take so long to run? How can it be made faster?

jvang 2018-07-19 17:18

[QUOTE=henryzz;492129]Why does that take so long to run? How can it be made faster?[/QUOTE]

Pretty sure the test cases for Haskell not only number in the hundreds, but are also quite large. I've heard that modulus is generally slow, and taking a very large power of two doesn't help...

jvang 2018-07-20 02:43

Worked some more with Linux stuff; I didn't complete the CentOS CLI installation so I had to restart mprime; the [FONT="Fixedsys"]history[/FONT] command was pretty useful since I mixed up the way to run the file...

I had a random USB that was formatted really weird for something, so we did some weird stuff to it. Not really sure what exactly happened; [FONT="Fixedsys"]fdisk[/FONT] for formatting, [FONT="Fixedsys"]badblocks[/FONT] to see if any blocks were messed up, I think [FONT="Fixedsys"]dd[/FONT] to write directly to the blocks from /dev/random and /dev/zero... not really sure what was done and why :leaving:

Haskell: More questions from my dad:

Is there a way to see exactly what is bound to a function/variable? If I type in x = 5, then call x, it prints 5. But if I write func x = x + 1, how do I see what is written there?

He also wanted to know what's up with types, especially Integral. Firstly, using Integral a => a -> a sets each instance of "a" to the type "Integral," which refers to numbers that support integer division. Num is a more general number type that includes floating point division. There's some weird superclass stuff that I saw while reading up on this that I don't understand yet, but that's for a later time.

Additionally, fromIntegral is a function that allows Integral inputs to be processed as Nums, which allows for floating point operations (sqrt, %, etc.) since Num has a weird non-support for the Eq superclass or something :unsure:

And an explanation of the (x:xs) syntax, as in func (x:xs); it turns the input into two parts, the original inputted list, xs, and the first element in it, x. This is commonly used in tail recusion, where cutting off the head of the input and recursively using it will end up processing the entire list.

I had a lot of trouble with the kata @henryzz linked. Having looked at the solutions I see that I was looking at it wrong, and his use of [FONT="Fixedsys"]zipWith[/FONT] was way weirder (and correct!) than what I was thinking. Very little of the solutions make sense, but I'm looking into each function used to try and figure out what's going on.

Nick 2018-07-20 08:40

[QUOTE=jvang;492160]
Is there a way to see exactly what is bound to a function/variable? If I type in x = 5, then call x, it prints 5. But if I write func x = x + 1, how do I see what is written there?
[/QUOTE]
You can use :t to see the type of a function but there is no general way to look up its source code.

[QUOTE=jvang;492160]
He also wanted to know what's up with types, especially Integral.
[/QUOTE]
Suppose we write a function for testing whether a number is even or odd,
and then ask ghci for the type of our function:
[CODE]
Prelude> odd n = if mod n 2 == 0 then False else True
Prelude> :t odd
odd :: Integral a => a -> Bool[/CODE]This means: for any integer type a, the function odd takes a value of type a and returns a value of type Bool.
So we could use the function with values of type Int or values of type Integer, for example,
but not with values of type Double (it doesn't make sense to ask if 1.5 is even or odd).
[QUOTE=jvang]
I've heard that modulus is generally slow, and taking a very large power of two doesn't help...
[/QUOTE]
You can learn more from henryzz's question than that!
Suppose you wanted to calculate \(2^6 \bmod 6\) by hand, for example.
If you were half asleep, you might do 5 multiplications and only then divide by 6 and take the remainder,
but if you were awake you would discover some shortcuts to reduce the amount of work!

jvang 2018-07-20 17:02

[QUOTE=Nick;492168]You can learn more from henryzz's question than that!
Suppose you wanted to calculate \(2^6 \bmod 6\) by hand, for example.
If you were half asleep, you might do 5 multiplications and only then divide by 6 and take the remainder,
but if you were awake you would discover some shortcuts to reduce the amount of work![/QUOTE]

Hmm, I tried looking for a pattern:

[CODE]Prelude> func x = mod (2^x) x
Prelude> map func [1..50]
[0,0,2,0,2,4,2,0,8,4,2,4,2,4,8,0,2,10,2,16,8,4,2,16,7,4,26,16,2,4,2,0,8,4,18,28,2,4,8,16,2,22,2,16,17,4,2,16,30,24][/CODE]

Replacing the 2nd x with a constant reveals some patterns (even exponents with mod (2^x) 6 result in 4 as a remainder, odd exponents return 2), but for the above case I can't tell much...

xilman 2018-07-20 19:28

[QUOTE=Nick;492168]You can use :t to see the type of a function but there is no general way to look up its source code.


Suppose we write a function for testing whether a number is even or odd,
and then ask ghci for the type of our function:
[CODE]
Prelude> odd n = if mod n 2 == 0 then False else True
Prelude> :t odd
odd :: Integral a => a -> Bool[/CODE]This means: for any integer type a, the function odd takes a value of type a and returns a value of type Bool.
So we could use the function with values of type Int or values of type Integer, for example,
but not with values of type Double (it doesn't make sense to ask if 1.5 is even or odd).

You can learn more from henryzz's question than that!
Suppose you wanted to calculate \(2^6 \bmod 6\) by hand, for example.
If you were half asleep, you might do 5 multiplications and only then divide by 6 and take the remainder,
but if you were awake you would discover some shortcuts to reduce the amount of work![/QUOTE]Hint: 6 = 2*3

Xyzzy 2018-07-21 21:57

We are having trouble with this: [url]https://www.codewars.com/kata/multiples-of-3-and-5-redux[/url]

Our first try timed out:[CODE]module Codewars.Kata.MultiplesRedux where
import Data.List (nub)
solution :: Integer -> Integer
solution x = sum $ nub [x | x <- ([0,3..x - 1] ++ [0,5..x - 1])][/CODE]Our second try completed some of the sample tests and then timed out:[CODE]module Codewars.Kata.MultiplesRedux where
solution :: Integer -> Integer
solution x = sum [0,3..x - 1] + sum [0,5..x - 1] - sum [0,15..x - 1][/CODE]Here is a help message from the system:[QUOTE]Why did my code time out?
Our servers are configured to only allow a certain amount of time for your code to execute. In rare cases the server may be taking on too much work and simply wasn't able to run your code efficiently enough. Most of the time though this issue is caused by inefficient algorithms. If you see this error multiple times you should try to optimize your code further. [/QUOTE]We just need a small hint towards a more efficient algorithm!

:help:

R. Gerbicz 2018-07-21 22:31

[QUOTE=Xyzzy;492254]Our second try completed some of the sample tests and then timed out:[CODE]module Codewars.Kata.MultiplesRedux where
solution :: Integer -> Integer
solution x = sum [0,3..x - 1] + sum [0,5..x - 1] - sum [0,15..x - 1][/CODE][/QUOTE]

The answer is good (in general setup it is the [url]https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle[/url]), just the summing is too slow.
So your only problem that you can't sum the (positive) integers that are divisibly by d up to N. Do you know the answer for d=1 ? Could you generalize it?

Xyzzy 2018-07-22 00:44

From looking at the results we see that as x increases, the sum of the 3s and 5s increases. It appears that both increase at (roughly) the same linear rate. So we need to find a constant to multiply with x that will yield the sum of the 3s and 5s?

:mike:


All times are UTC. The time now is 10:06.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.