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 e88004f

Browse files
Add Integer Complexity 1 → DAILY/Attempting; Write strategy notes
1 parent 5e67035 commit e88004f

File tree

5 files changed

+189
-10
lines changed

5 files changed

+189
-10
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ===================================
2+
// ProbName - Solution
3+
// ===================================
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Challenge #354 [Easy]: [Integer Complexity 1](https://www.reddit.com/r/dailyprogrammer/comments/83uvey/20180312_challenge_354_easy_integer_complexity_1/)
2+
3+
#### Date: 12-Mar-2018
4+
5+
Given a number A, find the smallest possible value of B+C, if B\*C = A. Here A, B, and C must all be positive integers. 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. Post the return value for A = 345678 along with your solution.
6+
7+
For instance, given A = 12345 you should return 838. Here's why. There are four different ways to represent 12345 as the product of two positive integers:
8+
9+
```
10+
12345 = 1*12345
11+
12345 = 3*4115
12+
12345 = 5*2469
13+
12345 = 15*823
14+
```
15+
16+
The sum of the two factors in each case is:
17+
18+
```
19+
1*12345 => 1+12345 = 12346
20+
3*4115 => 3+4115 = 4118
21+
5*2469 => 5+2469 = 2474
22+
15*823 => 15+823 = 838
23+
```
24+
25+
The smallest sum of a pair of factors in this case is 838.
26+
27+
Examples:
28+
29+
```
30+
12 => 7
31+
456 => 43
32+
4567 => 4568
33+
12345 => 838
34+
```
35+
36+
The corresponding products are:
37+
38+
```
39+
12 = 3*4
40+
456 = 19*24
41+
4567 = 1*4567
42+
12345 = 15*823
43+
```
44+
45+
## Hint
46+
47+
Want to test whether one number divides evenly into another? This is most commonly done with the modulus operator (usually %), which gives you the remainder when you divide one number by another. If the modulus is 0, then there's no remainder and the numbers divide evenly. For instance, 12345 % 5 is 0, because 5 divides evenly into 12345.
48+
49+
### Optional bonus 1
50+
51+
Handle larger inputs efficiently. You should be able to handle up to 12 digits or so in about a second (maybe a little longer depending on your programming language). Find the return value for 1234567891011.
52+
53+
Hint: _how do you know when you can stop checking factors?_
54+
55+
### Optional bonus 2
56+
57+
Efficiently handle very large inputs whose prime factorization you are given. For instance, you should be able to get the answer for 6789101112131415161718192021 given that its prime factorization is:
58+
59+
6789101112131415161718192021 = 3*3*3*53*79*1667*20441*19646663*89705489
60+
In this case, you can assume you're given a list of primes instead of the number itself. (To check your solution, the output for this input ends in 22.)
61+
62+
---
63+
64+
| Started | Last revisited | Completed |
65+
| ----------- | -------------- | ----------- |
66+
| 07-Sep-2018 | 07-Sep-2018 | dd-MMM-yyyy |

‎DailyProgrammer/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
## Need to Revisit
1010

11-
| Problem | Revisited | Code Link |
12-
| ------------ | ----------- | ----------------------------------- |
13-
| Problem name | dd-MMM-yyyy | [Problem](Update link after commit) |
11+
| Problem | Revisited | Code Link |
12+
| --------------------------------- | ----------- | ----------------------------------- |
13+
| #354[Easy]: Integer Complexity 1 | 07-Sep-2018 | [Problem](Update link after commit) |
1414

1515
---
1616

‎README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ My notes and answers as JS files are also available to show my answer and steps
1818

1919
### Last 5 Revisited
2020

21-
| Problem | Source | Revisited | Prompt & Code |
22-
| --------------------------------- | ------ | ----------- | -------------------------------- |
23-
| #25 - 1000-digit Fibonacci number | EULER | 02-Sep-2018 | [Prob 25](https://git.io/fARt7) |
24-
| #5 - Smallest multiple | EULER | 31-Jul-2018 | [Prob 5](https://git.io/fARtX) |
25-
| #22L - Names scores (lite) | EULER | 31-Jul-2018 | [Prob 22L](https://git.io/fARtH) |
26-
| #4 - Largest palindrome product | EULER | 30-Jul-2018 | [Prob 4](https://git.io/fARt6) |
27-
| #7 - 10 001st prime | EULER | 29-Jul-2018 | [Prob 7](https://git.io/fARtM) |
21+
| Problem | Source | Revisited | Prompt & Code |
22+
| --------------------------------- | ------ | ----------- | ----------------------------------- |
23+
| #354 [Easy]: Integer Complexity 1 | DAILY | 07-Sep-2018 | [Problem](Update link after commit) |
24+
| #25 - 1000-digit Fibonacci number | EULER | 02-Sep-2018 | [Prob 25](https://git.io/fARt7) |
25+
| #5 - Smallest multiple | EULER | 31-Jul-2018 | [Prob 5](https://git.io/fARtX) |
26+
| #22L - Names scores (lite) | EULER | 31-Jul-2018 | [Prob 22L](https://git.io/fARtH) |
27+
| #4 - Largest palindrome product | EULER | 30-Jul-2018 | [Prob 4](https://git.io/fARt6) |
28+
| #7 - 10 001st prime | EULER | 29-Jul-2018 | [Prob 7](https://git.io/fARtM) |
2829

2930
### [View Master List of Problems](https://git.io/fAz0p)
3031

0 commit comments

Comments
(0)

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