Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

n0sya's Math Problem of the Week: Week 1 (Subsets)

Hello fellow members,
I've decided to post weekly math challenges just for the hell of it. Each problem will be worth around one point, and whoever accumulates the most points gains bragging rights around these parts. Anyways, make sure to post your answer (along with how you got it - no cheating!) in spoiler tags. Your work must be your own, but there are no limits to what resources you can use.

That being said, have fun!

Problem 1 (1 point):
Spoiler (Click to View)

A correct solution will be posted on November 13th.
Spoiler (Click to View)
(Nov 6, 2016 07:27 PM)aimware Wrote:  Spoiler (Click to View)

Good attempt! That would be the amount of subsets for a set with 31 elements, but that's not what the question asks for 🤮
(Nov 6, 2016 06:54 PM)the wolf of blockchain Wrote:  Hello fellow members,
I've decided to post weekly math challenges just for the hell of it. Each problem will be worth around one point, and whoever accumulates the most points gains bragging rights around these parts. Anyways, make sure to post your answer (along with how you got it - no cheating!) in spoiler tags. Your work must be your own, but there are no limits to what resources you can use.

That being said, have fun!

Problem 1 (1 point):
Spoiler (Click to View)

A correct solution will be posted on November 13th.

That's pretty interesting, I'll take a crack. I'm going to type up my entire thought process.
Spoiler (Click to View)

This brought a bit of nostalgia, really, in hindset there's better ways to do this but I tackled this problem like I would've back when I did MathCounts in middle school.
(Nov 6, 2016 10:41 PM)Adlil Wrote:  That's pretty interesting, I'll take a crack. I'm going to type up my entire thought process.
-snip-

That was a very interesting way to tackle this mathematically! I'm impressed. To keep the spirit of the game, I won't say if it's right or wrong, but checking with an algorithm never hurts 👍
Can you clarify on your definition of a pair? It is ambiguous. I have already developed a solution, it is just a matter of fine-tuning with respect to your definition.

At the moment, I assume you define a pair as any set of the form {a, b} such that a = b-2 and b = a+2. You have not defined any particular relation, so I can only assume the pairs are unordered at this point. {0, 2} is clearly a pair, but how about a superset S = {0, 1, 2}? At this point, it is ambiguous, because no relation is defined and set equality allows the set to be rearranged: S = {0, 2, 1} = {1, 2, 0}.

Thanks. :)
[Image: 8Yoto.png]
(Nov 7, 2016 12:20 PM)Phizo Wrote:  Can you clarify on your definition of a pair?

To clarify, the question asks how many subsets exists such that no two elements in them differ by two. The set {1,2,5} would pass, but the set {1,2,3,5} would not. I hope this clears it up!
hello, the answer is (Click to View)
-wolga
That is actually not an easy question... the way I would solve it is to set up a recurrence relation. Let a(n) denote the # of such subsets from the set {0,1,2,...,n}. The number that we're interested is denoted by a(31), I imagine. So the way I see how to solve this is to divide the problem into two cases. When set contains the #31 and when it does not. When it does not we need to count the number of subsets of {0,1,2,...,30} which is a(30). And when it does contain 31 we need to count the subsets of the form {31,...} which is related to a(29), but not quite, we would need to fix a small counting issue, and so the recursion is roughly; a(n) is a sum of a_(n-1) and a_(n-2).

Does that make any sense to anyone else?
(Nov 8, 2016 01:42 AM)imajerk Wrote:  That is actually not an easy question... the way I would solve it is to set up a recurrence relation. Let a(n) denote the # of such subsets from the set {0,1,2,...,n}. The number that we're interested is denoted by a(31), I imagine. So the way I see how to solve this is to divide the problem into two cases. When set contains the #31 and when it does not. When it does not we need to count the number of subsets of {0,1,2,...,30} which is a(30). And when it does contain 31 we need to count the subsets of the form {31,...} which is related to a(29), but not quite, we would need to fix a small counting issue, and so the recursion is roughly; a(n) is a sum of a_(n-1) and a_(n-2).

Does that make any sense to anyone else?

Spoiler (Click to View)
I'm fairly certain that this is from abstract algebra, and I really don't want to travel back to those theorems. Good luck to everyone trying it out.
2 ez tbh imo etc et al. :\

I decided to solve it mostly using first-order predicate calculus, computationally in Prolog, which I did follow up with some handwork.



Here is an illustration of the program that computes all solutions in P([0, 5]), where P is a function which maps a given set into a powerset (the set of all subsets):
PHP Code

<?php 
| ?- [n0sya].                            
compiling /private/tmp/n0sya.pl for byte code...
/private/
tmp/n0sya.pl compiled, 21 lines read - 3838 bytes written, 6 ms

(1 ms) yes
| ?- make_list(0, 5, L), maplist(go, L). 

Reduced powerset: [[0],[]]
Number of subsets: 2

Reduced powerset
: [[0,1],[1],[],[0]]
Number of subsets: 4

Reduced powerset
: [[1,2],[2],[],[1],[0],[0,1]]
Number of subsets: 6

Reduced powerset
: [[2,3],[3],[],[2],[1],[1,2],[0,3],[0],[0,1]]
Number of subsets: 9

Reduced powerset
: [[3,4],[4],[],[3],[2],[2,3],[1,4],[1],[1,2],[0,3,4],[0,4],[0],[0,3],[0,1,4],[0,1]]
Number of subsets: 15

Reduced powerset
: [[4,5],[5],[],[4],[3],[3,4],[2,5],[2],[2,3],[1,4,5],[1,5],[1],[1,4],[1,2,5],[1,2],[0,4,5],[0,5],[0],[0,4],[0,3],[0,3,4],[0,1,4,5],[0,1,5],[0,1],[0,1,4]]
Number of subsets: 25 

It can solve for:
  • [0, 10] in ~7ms.
  • [0, 15] in ~179ms
  • [0, 20] in ~6 seconds
  • [0, 21] in ~12 seconds
  • [0, 22] in ~24 seconds
  • [0, 24] in ~1.9 minutes
  • [0, 25] in ~3.47 minutes
  • [0, 26] in ~6.8 minutes
As you may notice, an increment in set cardinality causes an increase in computational time by a factor of ~2.09 on average, which can be represented functionally by t(f(n+k)) = t(f(n)) 2.09^k, where t is a function that measures computational time and f = go (my top-level Prolog predicate).

That is, the running time is O(2^n) (exponential with a linear exponent), which is fairly inefficient. Note that Prolog (and logic programming in general) is not concerned with runtime efficiency: the declarative paradigm promotes theorem-proving, and consequently program correctness, to programmers and proof procedures, which are often computationally expensive.

As it is such a computationally complex problem, I cannot practically compute the solution for the set [0, 31], as it will take t(f(31)) ~ t(f(26))
2.09^5 = ~4.5 hours.
So, I will compute the rest of the solution by hand. :)

Spoiler (Click to View)
[Image: 8Yoto.png]
(Nov 8, 2016 02:27 PM)Phizo Wrote:  2 ez tbh imo etc et al. :\
-big snip-

Very well done! You've laid out an intricate processes to get your solution (even picking up what Adlil left off!), and for some reason it reminds me of a Strong Proof back in discrete. As always, I won't say if it's right or wrong, but I'm going to challenge myself to form a solution with pure mathematics.
(Nov 8, 2016 03:25 PM)the wolf of blockchain Wrote:  Very well done! You've laid out an intricate processes to get your solution (even picking up what Adlil left off!), and for some reason it reminds me of a Strong Proof back in discrete. As always, I won't say if it's right or wrong, but I'm going to challenge myself to form a solution with pure mathematics.

You can easily form a purely mathematical solution by hypothesising f's rules (as I did) and then proving them using mathematical induction, which is what I planned to do at the end, but decided I couldn't be bothered after everything I had written. I would argue that my solution is purely mathematical despite that, though. I defined my solutions in terms of branches of mathematical logic: first-order logic and set theory, respectively. The first-order logic is just abstracted by a notational system for describing computation (i.e. a programming language), and the set theory (mostly functions/relations) assumes definitions described by the logic, as redefinition is simply a redundancy.

Thanks for the compliments. Please keep the challenges coming. :)

For anyone who is interested, I came up with a simple solution to dramatically speed up the problem solver I developed. I incorporated an optimisation technique called memoisation. Technically, it is no longer pure logic programming by incorporating this. Without this optimisation: for every call to f(n), f(x) is also called for all x in [0, (n-1)!] (excluding the base case f(-1)). This is expensive when you consider, if you want f(31), you call f(26) 5 times, which takes ~34 minutes. So, imagine with all the rest of the calls to f (hence why it takes ~4.5 hours). With the optimisation: for every call to f(n), f(x) is also called for all x in [0, n-1]. This basically allows each solved subproblem to be stored for reuse, so that it is not recomputed ever again. Essentially, the factorial function f is now linear.

I verified that the computed f(31) matches my answer.
Solving for f(80) instantly:
PHP Code

<?php 
| ?- [n0ysa2].    
compiling /private/tmp/n0ysa2.pl for byte code...
/private/
tmp/n0ysa2.pl compiled, 42 lines read - 7219 bytes written, 7 ms

(2 ms) yes
| ?- go(80).            
Number of subsets: 116139356908771352

yes 

These are the code changes (impure code is often fairly gross):
[Image: 8Yoto.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by zFacetiousOct 3, 2022 04:25 PM 4 182 Oct 3, 2022 04:25 PM
Last Post: manmafia_
[College] by SIM-3092053Mar 11, 2019 02:09 PM 1 171 Mar 11, 2019 02:09 PM
Last Post: UrLocalNewbie
by OffsetOct 31, 2018 01:56 PM 2 313 Oct 31, 2018 01:56 PM
Last Post: Offset