-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdigitsProduct.R
More file actions
executable file
·157 lines (143 loc) · 4.22 KB
/
digitsProduct.R
File metadata and controls
executable file
·157 lines (143 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Given an integer product, find the smallest positive (i.e. greater than 0)
# integer the product of whose digits is equal to product. If there is no such
# integer, return -1 instead.
#
# Example
#
# For product = 12, the output should be digitsProduct(product) = 26; For
# product = 19, the output should be digitsProduct(product) = -1.
#
# Input/Output
#
# [execution time limit] 4 seconds (py3)
#
# [input] integer product
#
# Guaranteed constraints: 0 ≤ product ≤ 600.
#
# [output] integer
#attempt3: attempt2 worked but not very elegant solution.
#trying another time.
#where u dont have to merge.
#good solution: all that was needed was to break out of the loop
#each time a factor was found, so that u dont split into smaller
#factors as long as a bigger factor exists (between 2-9)
digitsProduct <- function(product) {
if (product == 1) {
return(1)
}
if (product == 0) {
return(10)
}
numbertodivide = product
finalnumber <- c()
factors <- c()
while (numbertodivide > 1) {
somefactor = FALSE
for (ind in seq(from = 9, to = 2, by = -1)) {
if (numbertodivide %% ind == 0) {
numbertodivide = (numbertodivide / ind)
factors <- c(factors,ind)
somefactor = TRUE
break
}
}
if (!somefactor) {
return(-1)
}
}
return(as.numeric(paste(sort(factors),collapse = "")))
}
#attempt2:
#1. get prime factors
#it worked but I am not happy with the solution yet.
#esp with the merging part, coz it only does 2 digits merge
#not all possible.
#need to work on that.
digitsProduct <- function(product) {
if (product == 1) {
return(1)
}
if (product == 0) {
return(10)
}
numbertodivide = product
finalnumber <- c()
divideby2to9 <- function(x) {
factors = c()
for (ind in seq(from = 9, to = 2,by = -1)) {
if (x %% ind == 0) {
x = (x / ind)
factors <- c(factors,ind)
}
}
return(list(x,factors))
}
while (numbertodivide > 1) {
output <- divideby2to9(numbertodivide)
numbertodivide = output[[1]]
finalnumber <- c(finalnumber,output[[2]])
if (output[[1]] == numbertodivide & is.null(output[[2]])) {
return(-1)
}
}
#now make the smallest number out of finalnumber digits
#if any digits can be clubbed into a single digits < 9, then
#do that and find the product. compute the min of those combinations
mergedigits <- function(x) {
#replace 2,2 by 4
#replace 2,3 by 6
#replace 3,3 by 9
if (length(grep("2|3|4",x)) >= 2) {
allreplacements = combn(grep("2|3|4", x),2)
allfinalnumbers <- c()
for (ind in seq(1:ncol(allreplacements))) {
if (prod(x[allreplacements[,ind]]) <= 9) {
newfinalnumber <- c(x[-(c(allreplacements[,ind]))], prod(x[allreplacements[,ind]]))
#make it min (sort it and collapse it)
allfinalnumbers <- c(allfinalnumbers,
as.numeric(paste(sort(newfinalnumber),
collapse = "")))
}
}
return(allfinalnumbers)
}
}
morefinalnumbers <- c(as.numeric(paste(sort(finalnumber),collapse = "")),
mergedigits(finalnumber))
return(min(morefinalnumbers))
}
#this gets the smallest number but the problem asks for
#the number where product of each bit is the answer
#for e.g. for product = 450, output the following code
# would generate is 509, however what the problem is seeking
# is 2559 as 5 * 0 * 9 is not 450 but 2 * 5* 5 * 9 = 450
#thus we are looking at factors between 2 to 9.
# digitsProduct <- function(product) {
# allproducts <- vector()
# index = 1;
# for (ind in 2:(product - 1)) {
# if (product %% ind == 0) {
# #get quotient
# quotient = (product / ind)
# #combine
# smallestproduct = paste0(ind,
# quotient,
# collapse = "")
# allproducts[index] <- as.numeric(smallestproduct)
# index <- index + 1;
# }
# }
# allproducts = allproducts[allproducts > 0]
# if (length(allproducts) > 0) {
# return(min(allproducts))
# } else {
# return(-1)
# }
# }
digitsProduct(19)
digitsProduct(13)
digitsProduct(12)
digitsProduct(36)
digitsProduct(450)
digitsProduct(576)