Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef01f8a

Browse files
Add DAILY/364
1 parent 7faed74 commit ef01f8a

File tree

3 files changed

+290
-0
lines changed

3 files changed

+290
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// ===================================
2+
// Integer Complexity 1 - Comparative Solution
3+
// ===================================
4+
5+
// Based on strategy outlined in notes, we can quickly find factors
6+
7+
function intComplexityOne(number) {
8+
// default value: number * 1 ALWAYS = number
9+
// therefore we can start at number + 1 and work downwards if factors exist
10+
let lowestFactorSum = number + 1;
11+
12+
// Define upper bound for loop based on strategy in notes
13+
let factorUpperBound = Math.floor(Math.sqrt(number));
14+
// let factorUpperBound = 351364;
15+
16+
// take each integer between one and the factorUpperBound...
17+
for (let i = 1; i <= factorUpperBound; i++) {
18+
// ...and see if it divides evenly into the number.
19+
if (number % i === 0) {
20+
// if it does, take i's factor pair...
21+
let pair = number / i;
22+
23+
// ...add it to i to get the pair sum
24+
let pairSum = pair + i;
25+
26+
// if current pairSum is less than global lowestFactorSum...
27+
if (pairSum < lowestFactorSum) {
28+
// ...assign factorSum the value of current pairSum
29+
lowestFactorSum = pairSum;
30+
}
31+
}
32+
}
33+
// return the lowest factor sum;
34+
return lowestFactorSum;
35+
}
36+
37+
// TEST CASES
38+
// intComplexityOne(12); //=> 7 [CORRECT]
39+
// intComplexityOne(456); //=> 43 [CORRECT]
40+
// intComplexityOne(4567); //=> 4567 [CORRECT]
41+
// intComplexityOne(12345); //=> 838 [CORRECT]
42+
43+
// ANSWER
44+
// intComplexityOne(345678); //=> 3491 [CORRECT]
45+
46+
// BONUS
47+
// intComplexityOne(1234567891011); //=> 4695212 [NOPE]
48+
49+
// ===================================
50+
// Integer Complexity 1 - Old Array and Sorting Solution
51+
// ===================================
52+
53+
// Original solution that collected ALL sums, stored them, sorted them
54+
// and returned the first value of the stored sums array (would be lowest)
55+
56+
// Based on strategy outlined in notes, we can quickly find factors
57+
58+
function intComplexityOne(number) {
59+
// default value - we can assume number > 1
60+
// initial array to hold arrays of factor pairs
61+
let factorSumArr = [];
62+
63+
// Define upper bound for loop based on strategy in notes
64+
let factorUpperBound = Math.floor(Math.sqrt(number));
65+
66+
// take each integer between one and the factorUpperBound...
67+
for (let i = 1; i <= factorUpperBound; i++) {
68+
// ...and see if it divides evenly into the number.
69+
if (number % i === 0) {
70+
// if it does, take i's factor pair...
71+
let pair = number / i;
72+
73+
// ...add it to i...
74+
let pairSum = pair + i;
75+
76+
// ...and push that value to the factorSum array
77+
factorSumArr.push(pairSum);
78+
}
79+
}
80+
// at this point all factor pairs have been added together and pushed into the factorSum array
81+
// return the lowest number from this array of values
82+
let sorted = factorSumArr.sort((a, b) => {
83+
return a - b;
84+
});
85+
86+
// return factorSumArr[0];
87+
return sorted[0];
88+
}
89+
90+
intComplexityOne(12345); //=> 838 [CORRECT]
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Challenge #354 [Easy]: Integer Complexity - Notes
2+
3+
From the prompt:
4+
5+
> It's okay to use brute force by checking every possible value of B and C. You don't need to handle inputs larger than six digits.
6+
7+
Brute force sounds like fun but I think I have an idea regarding how to eliminate large numbers of test cases.
8+
9+
### Test Case: 12
10+
11+
Consider the test case of 12 and its factor pairs as shown below:
12+
13+
```
14+
1 x 12
15+
2 x 6
16+
3 x 4
17+
```
18+
19+
There are three pairs of factors, but technically, these pairs have pairs:
20+
21+
```
22+
4 x 3
23+
6 x 2
24+
12 x 1
25+
```
26+
27+
If we start testing for factors from 1, our upperbound will be the original number aka `1 x 12`.
28+
29+
However, when we move to 2, 6 presents itself as a factor. Since we are only dealing with positive integers, our upperbound has been reduced from 12 to 6 – the proof being that when we increment our base divisor by, we get a nice integer factor.
30+
31+
```
32+
BASE UPPER BOUND
33+
1 12 (original number)
34+
2 6
35+
3 4
36+
```
37+
38+
We can continue this pattern while (base <= divisor). After that, we begin mirroring results.
39+
40+
```
41+
BASE UPPER BOUND
42+
1 12 (original number)
43+
2 6
44+
3 4 (last case where base <= divisor)
45+
-------
46+
4 3 (first case of mirroring - further results meaningless)
47+
```
48+
49+
### Test Case: 36
50+
51+
Let's enumerate factor pairs and see where mirroring begins:
52+
53+
```
54+
BASE UPPER BOUND
55+
1 36 (original number)
56+
2 18
57+
3 12
58+
4 9
59+
6 6 (last case where base <= divisor)
60+
-------
61+
9 4 (first case of mirroring - further results meaningless)
62+
```
63+
64+
36 is a square so base 6 has divisor 6.
65+
66+
If we were to do some arbitrary division like `36 / 6.01`, the answer would be ~5.99, meaning that the base (6.01) is larger than the divisor (5.99). Thus, the upper bound of our brute force is equal to:
67+
68+
```js
69+
upperBound = Math.floor(Math.sqrt(inputNumber));
70+
```
71+
72+
Examples:
73+
74+
### Test Case: 18
75+
76+
```
77+
upperBound = Math.floor(Math.sqrt(18));
78+
= Math.floor(4.2)
79+
= 4
80+
81+
BASE UPPER BOUND
82+
1 18 (original number)
83+
2 9
84+
3 6 (last case where base <= divisor)
85+
-- 4 -- (mirror test upperBound)
86+
6 3 (first case of mirroring - further results meaningless)
87+
88+
upperBound test pass; 18 - 4 = 14 numbers saved from testing
89+
```
90+
91+
### Test Case: 140
92+
93+
```
94+
upperBound = Math.floor(Math.sqrt(140));
95+
= Math.floor(11.8)
96+
= 11
97+
98+
BASE UPPER BOUND
99+
1 140 (original number)
100+
2 70
101+
4 35
102+
5 28
103+
7 20
104+
10 14 (last case where base <= divisor)
105+
-- 11 -- (mirror test upperBound)
106+
14 10 (first case of mirroring - further results meaningless)
107+
108+
upperBound test pass; 140 - 11 = 129 numbers saved from testing
109+
```
110+
111+
Don't even need internal array of factors; just add them together on the spot.
112+
113+
### Sorting that array
114+
115+
```js
116+
factorSumArr.sort(
117+
// returns array with numbers sorted from low to high
118+
(a, b) => {
119+
return a - b;
120+
}
121+
);
122+
```
123+
124+
From here, we can just return the first entry
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Challenge #364 [Easy]: [Create a dice roller](https://www.reddit.com/r/dailyprogrammer/comments/8s0cy1/20180618_challenge_364_easy_create_a_dice_roller/)
2+
3+
#### Date: 18-Jun-2018
4+
5+
#### Attempting: 24-Sep-2018
6+
7+
### Setup
8+
9+
I love playing D&D with my friends, and my favorite part is creating character sheets (my DM is notorious for killing us all off by level 3 or so). One major part of making character sheets is rolling the character's stats. Sadly, I have lost all my dice, so I'm asking for your help to make a dice roller for me to use!
10+
11+
### Formal Inputs & Outputs
12+
13+
Input description
14+
Your input will contain one or more lines, where each line will be in the form of "NdM"; for example:
15+
16+
```
17+
3d6
18+
4d12
19+
1d10
20+
5d4
21+
```
22+
23+
If you've ever played D&D you probably recognize those, but for the rest of you, this is what those mean:
24+
25+
The first number is the number of dice to roll, the d just means "dice", it's just used to split up the two numbers, and the second number is how many sides the dice have. So the above example of "3d6" means "roll 3 6-sided dice". Also, just in case you didn't know, in D&D, not all the dice we roll are the normal cubes. A d6 is a cube, because it's a 6-sided die, but a d20 has twenty sides, so it looks a lot closer to a ball than a cube.
26+
27+
The first number, the number of dice to roll, can be any integer between 1 and 100, inclusive.
28+
29+
The second number, the number of sides of the dice, can be any integer between 2 and 100, inclusive.
30+
31+
Output description
32+
You should output the sum of all the rolls of that specified die, each on their own line. so if your input is "3d6", the output should look something like:
33+
34+
```
35+
14
36+
```
37+
38+
Just a single number, you rolled 3 6-sided dice, and they added up to 14.
39+
40+
### Challenge Input
41+
42+
```
43+
5d12
44+
6d4
45+
1d2
46+
1d8
47+
3d6
48+
4d20
49+
100d100
50+
```
51+
52+
### Challenge Output
53+
54+
```
55+
[some number between 5 and 60, probably closer to 32 or 33][some number between 6 and 24, probably around 15]
56+
[you get the idea][...]
57+
```
58+
59+
### Notes/Hints
60+
61+
A dice roll is basically the same as picking a random number between 1 and 6 (or 12, or 20, or however many sides the die has). You should use some way of randomly selecting a number within a range based off of your input. Many common languages have random number generators available, but at least a few of them will give the same "random" numbers every time you use the program. In my opinion that's not very random. If you run your code 3+ times with the same inputs and it gives the same outputs, that wouldn't be super useful for a game of D&D, would it? If that happens with your code, try to find a way around that. I'm guessing for some of the newer folks, this might be one of the trickier parts to get correct.
62+
63+
Don't just multiply your roll by the number of dice, please. I don't know if any of you were thinking about doing that, but I was. The problem is that if you do that, it eliminates a lot of possible values. For example, there's no way to roll 14 from 3d6 if you just roll it once and multiply by 3. Setting up a loop to roll each die is probably your best bet here.
64+
65+
### Bonus
66+
67+
In addition to the sum of all dice rolls for your output, print out the result of each roll on the same line, using a format that looks something like:
68+
69+
```
70+
14: 6 3 5
71+
22: 10 7 1 4
72+
9: 9
73+
11: 3 2 2 1 3
74+
```
75+
76+
You could also try setting it up so that you can manually input more rolls. that way you can just leave the program open and every time you want to roll more dice, you just type it in and hit enter.

0 commit comments

Comments
(0)

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