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 060e489

Browse files
Complete primary solution: DAILY\Completed → 354
1 parent 5e82f25 commit 060e489

File tree

11 files changed

+121
-50
lines changed

11 files changed

+121
-50
lines changed

‎DailyProgrammer/Attempting/354: Integer Complexity 1/354-IntegerComplexity1.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
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]

‎DailyProgrammer/Attempting/354: Integer Complexity 1/354: Integer Complexity 1 - Notes.md renamed to ‎DailyProgrammer/Completed/354_Integer-Complexity-1/354_Integer-Complexity-1.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,17 @@ BASE UPPER BOUND
108108
upperBound test pass; 140 - 11 = 129 numbers saved from testing
109109
```
110110

111-
Don't even need internal array; just add on the spot.
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

‎DailyProgrammer/Attempting/354: Integer Complexity 1/README.md renamed to ‎DailyProgrammer/Completed/354_Integer-Complexity-1/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Date: 12-Mar-2018
44

5+
#### Completed: Regular Solution only - Bonuses need work
6+
57
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.
68

79
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:
File renamed without changes.
File renamed without changes.

‎DailyProgrammer/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
## Completed
44

5-
| Problem | Completed | Code Link |
6-
| ---------------------- | ----------- | ------------------------------- |
7-
| #76: Title Case [Easy] | 05-Sep-2018 | [Problem](https://git.io/fAREH) |
5+
| Problem | Completed | Code Link |
6+
| --------------------------------- | ----------- | --------------------------------------- |
7+
| #354 [Easy]: Integer Complexity 1 | 07-Sep-2018 | [Problem](Add link → move to completed) |
8+
| #76 [Easy]: Title Case | 05-Sep-2018 | [Problem](Update link → rename folder) |
89

910
## Need to Revisit
1011

11-
| Problem | Revisited | Code Link |
12-
| --------------------------------- | ----------- | ------------------------------- |
13-
| #354[Easy]: Integer Complexity 1 | 07-Sep-2018 | [Problem](https://git.io/fAzMg) |
12+
| Problem | Revisited | Code Link |
13+
| ------------------------- | ----------- | ---------------------------- |
14+
| #### [DIFF]: Problem Name | dd-MMM-yyyy | [Problem](Add link → commit) |
1415

1516
---
1617

@@ -59,3 +60,4 @@ Base folder, js, and md folder templates"
5960
```
6061

6162
[How a message committed like this looks](https://git.io/fAWiC)
63+
`

0 commit comments

Comments
(0)

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