This is your Sup Forums interview. Prove you are not a brainlet.
Sum all dubs, trips and quads below two million.
This is your Sup Forums interview. Prove you are not a brainlet.
Sum all dubs, trips and quads below two million.
Other urls found in this thread:
docs.python.org
twitter.com
H-hello!
Code artisan here. Solution coming momentarily
>This is your Sup Forums interview.
>Prove you are not a brainlet.
These don't go together.
this should do it
$sum = 0;
for($i = 0; $i < 2000000; $i++){
$y = 0;
for($x = count((string)$i) - 2; $x >= 0; $x--) {
if(((string)$i)[$x] == ((string)$i)[count((string)$i)-1]) $y++;
}
if($y > 1) $sum += $i;
}
function repetitionValue(num) {
let charDigits = num.toString().split('');
let initChar = charDigits[0];
let finalString = initChar;
for(let i = 1; i < charDigits.length; i++) {
if(charDigits[i] === initChar) {
finalString += charDigits[i];
} else {
break;
}
}
if(finalString.length < 2) {
return 0;
} else {
return Number(finalString);
}
}
let sum = 0;
for(let i = 11; i < 2000000; i++) {
sum += repetitionValue(i);
}
console.log(sum);
dawg where's the READABILITY
twas but a joke my good man
secretly impressed. it's all good. i've been watching a lot of funfunfunction and he talks a lot about what it's like to be a working dev, and how most time is spent making sense of other people's code. so when i practice even with toy problems like this i do my best to strive for that there ez to read structure. hopefully one day i'll make someone's job a little easier
>using strings
I thought that picture was a product photo of a laptop display. I've been spending too much time on eBay.
I could easily do it with string functions but that kinda defeats the purpose.
while(true) { Console.WriteLine("OP is a faggot"); }
digits = lambda n, x: x % 10**n % sum(map(lambda n: 10**n, range(n))) == 0
print(sum(filter(lambda x: digits(2, x) or digits(3, x) or digits(4, x), range(2000000))))
now thinking about it this is probably what you meant
digits = lambda n, x: x % 10**n % sum(map(lambda n: 10**n, range(n))) == 0
for i, s in enumerate(["dubs", "trips", "quads"]):
print('%s: %d' % (s, sum(filter(lambda x: digits(i + 2, x), range(2000000)))))
In Python 3
How do I make it not fail if i_list[n-1/2/3] is not there?
sum = 0
i = 0
while i < 2000000:
i = str(i)
i_list = []
for x in i:
i_list.append(x)
n = len(i_list)
# Checks if dubs
if i_list[n-1] == i_list[n-2] and i_list[n-2] != i_list[n-3]:
sum += i
# Checks if trips
elif i_list[n-1] == i_list[n-2] and i_list[n-2] == i_list[n-3] and i_list[n-3] != i_list[n-4]:
sum += i
# Checks if quads
elif i_list[n-1] == i_list[n-2] and i_list[n-2] == i_list[n-3] and i_list[n-3] == i_list[n-4] and i_list[n-4] != i_list[n-5]:
sum += i
i += 1
print(sum)
Guys you just sum i*11 for 1 to floor(2000000/11)
def f(n):
c = 0
for i in range(n+1):
s = str(i)
if len(s) >= 2 and s[-1]==s[-2]:
c += i
return c
result = 0
for x in range(11,2000000):
if str(x)[-1]==str(x)[-2]:
result+=x
print(result)
Check the length. You can do it like since it will short circuit and the list indexing will only evaluate if length check passes.
Also in python
A == B and B == C and C == D
can be written
A == B == C == D
Same with less than, low < x < high is valid.
fug, I realize what I did wrong.
just change the number
quads get
Pseudocode. I think it's correct, but untested.
n = 2000000
j = 1
For (i=1, i
Oops
x = (x * (x+1)) / 2
And
Print j * x
(((9*9)*5.5) + (9*5.5)*19999) + ((50*(19999*19999)) + (50*19999))
But not quints or higher?
Explanation?
every check less than or equal to Arg
Correction
(((9*9)*0.5) + (9*0.5)*19999) + ((5*(199999*199999)) + (5*199999))
Doubles
So many code babbies in this thread...
Observation: trips and quads are also dubs, so we need only consider the problem of summing all the dubs.
We want to find the value of S where S = 11 + 22 + ... + 133 + 144 + ... + 1999988 + 1999999.
Break each term into (something * 100 + ):
S = (0 * 100 + 11) + (0 * 100 + 22) + ... + (1 * 100 + 33) + (1 * 100 + 44) + ... + (19999 * 100 + 88) + (19999 * 100 + 99)
Group the multiples of 11 (each one appears 20000 times) and the multiples of 100 (each one appears 10 times):
S = 20000 * (11 + 22 + .. + 99) + 10 * (100 + 200 + ... + 1999900)
Take out common factors, we're left with two sums of consecutive integers, use the closed formula for that sum:
S = 11 * 20000 * (10 * 9 / 2) + 10 * 100 * (20000 * 19999 / 2)
OP wasn't very clear in what he wanted.
>Prove you are not a brainlet.
Most people failed.
>1999999
is not a quad you stupid fuck
Off by 9 million.
> i += 1;
# of dubs between 1 and 101:
10
# of dubs, trips and quads between 1 and 10,001:
10 * 100 = 1000
# of dubs, trips and quads less than 2 million:
(need to exclude any repeating digits that have more than 4 [quads] digits)
(1000 * 200) - 200
Result: 199800
># of dubs, trips and quads less than 2 million:
Correction (result is identical)
Matching against values less than 2,000,000 (1,999,999):
((1000 * 200) - 1) - 199
test
>So many code babbies in this thread...
>in the interview thread
wew
That's not /sci/.
Prove you are not a BRAINnet or Google Brain.
Is this the correct result?
All of these are incorrect. How does Sup Forums get it so wrong?
...
The actual answer is 199799900100, anyone who didn't get that needs to review their comprehension and coding skills.
>SUM all dubs, trips and quads below two million
>99999 is NOT dubs, trips, or quads
I might need a few whiteboards and days, but I've got this.
00+11+22+33+44+55+66+77+88+99+011+022+033+044+055+066+077+088+099+111+122+133+144+155......=2000000=ANS
I got 180000405000.
bool quints_or_more(int n){
if(n>11110){
if(n%100000%11111==0)//quints
return false;
if(n%1000000%111111==0)//sixts
return false;
return true;
}
}
int i;
in sum=0;
for(i=11;i
How did you arrive at that?
c = 0
for i in range(2000000):
s = str(i)
if len(s) >= 2 and s[-1]==s[-2]:
if len(s) > 4 and s[-1] == s[-5]:
continue
c += i
print i
Looking at output it seems to be counting the right ones.
>26171035
...
the '!' in line 26 shouldn't be there, and the if statement in line 28 should be moved outside the 'if(quints_or_more){'.
then it returns 199793729055
>if len(s) >= 2 and s[-1]==s[-2]:
if len(s) > 4 and s[-1] == s[-5]:
continue
Skips the dubs case where the fifth digit is equal to the dubs value, e.g.
30033
You are also printing every matching value
def sum(i):
s = 0;
for x in range(0, i):
xs = str(x)
xsl = len(xs)
if xs[xsl-2:xsl-1] == xs[xsl-1:xsl]:
if xs[xsl-3:xsl-2] != xs[xsl-2:xsl-1]:
s += x
elif xs[xsl-4:xsl-3] != xs[xsl-3:xsl-2]:
s += x
elif xs[xsl-5:xsl-4] != xs[xsl-4:xsl-3]:
s += x
return s
print sum(2000000)
#result -> 199799900100
>i ++
we want the case where you don't iterate through every value.
Loop (i) 1-9, 20,000 times.
First loop add i*11 to the total.
Second loop add i*111to the total.
Third loop till the end add i*1111 to the total.
0 is obviously ignored.
Does not validly cover values greater than 9*1111
But we are only going to quads so it's fine.
yeah ur fucking stupid
>/out/
No, you're fucking stupid.
It will loop the correct number of times adding the correct number of quads past 9999.
End yourself.
(((19999*100+11)+11)*(19999*100+1)/2)*((1+9)*9/2)
Given that each quad and trip is a dub too (you did not specify otherwise) I only need to sum all dubs and sum of all dubs is an arithmetic progression sum multiplied by sum of arithmetic progression from 1 to 9.
what the fuck are you guys doing? ever heard of this: 1+2+3+4+5+..+n = n*(n+1)/2
The set of all dubs, trips, quads, quints under 2000000 is the same as the set of all numbers up to 20000 paired up with the dubs like so ((number under 20000)*100 + dubs).
Also, 100*(1+2+..+n) = 50*n*(n+1).
Let N = 20.000
So for every dubs:
sum = 50*N*(N+1) + N*dubs
Taking this for all the dubs:
100*N*(N+1) + N*(00+11+22+33+44+55+66+77+88+99) = 40.011.900.000
Lols, I am fucking stupid.
Loop (i) 1-9, 3 times
First loop add i*11 to the total.
Second loop add i*111to the total.
Third loop till the end add i*1111 to the total.
And loop the whole thing about 200 times.
Oh and I forgot about 00s so
(((19999*100+11)+11)*(19999*100+1)/2)*((1+9)*9/2)+(2000000+0)*1999999/2
How about:
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(2) for x in xrange(9)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
max = 2000000
digits_list = []
for i in xrange(max):
if (
i % 100 in dubs or
i % 1000 in trips or
i % 10000 in dubs
):
digits_list.append(i)
print sum(digits_list)
I'm getting 179998920000.
That quoting was by accident.
Oh wait, I guess that should be xrange(max + 1).
With that I'm getting 180000920000.
Oops I did 2.000.000 instead of 1.999.999 this is easily fixed by just subtracting 2.000.000 from the whole thing, and also I sum a bit too many namely the (100*N + dubs) numbers.
100*N*(N+1) + N*(11+22+33+44+55+66+77+88) - (10*100*N + (11+22+33+44+55+66+77+88)) - 100*N = 39.987.919.604
oops now I subtracted N*100 twice,
N=20000,
100*N*(N+1) + N*(11+22+33+44+55+66+77+88) - (10*100*N + (11+22+33+44+55+66+77+88)) = 39.989.919.604
So are there even 2 answers in this thread that agree with eachother?
(sum [x*10+x|x
I have a question.
Let's say we have 1993333
Do I:
* Count it once
* Count it 3 times for dubs, trips and quads
You're fired
If we are not counting quads as also trips and dubs, and trips as also dubs, then the correct total is:
11097000
I am too tired to modify my loops to do counting quads as trips and dubs.
That's a serious question, depending on what the answer is the implementation needs to be different.
Okay, if we are counting each dub, trip and quad separately, even if they are part of the same number the implementation is this:
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(size) for x in xrange(9)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
max = 2000000
digits_list = []
for i in xrange(max + 1):
if i % 100 in dubs:
digits_list.append(i)
if i % 1000 in trips:
digits_list.append(i)
if i % 10000 in quads:
digits_list.append(i)
print sum(digits_list)
And the solution is 199802911200.
Here is my solution, my result is:
221,999,889,000
sum = 0
dubs = (0..99).step(11).to_a
trips = (0..999).step(111).to_a
quads = (0..9999).step(1111).to_a
# add dubs
(0...2_000_000).step(100).each do |n|
dubs.each do |dub|
sum += (n+dub)
end
end
# add trips
(0...2_000_000).step(1_000).each do |n|
trips.each do |trip|
sum += (n+trip)
end
end
# add quads
(0...2_000_000).step(10_000).each do |n|
quads.each do |quad|
sum += (n+quad)
end
end
puts sum
99 + 11 = 110; skipping 100 and not validly reaching 111.
>Lols, I am fucking stupid.
k
Try considering edge cases next time bro.
quads != dubs != trips
Its a single entry.
heh
Under that assumption the count is: 180000920000
I think this is wrong. How did you get that?
You know, I fucked up again, but not in the way you said. Your interpretation of what my loops were doing is fucking wrong so you're just as fucking retarded.
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(size) for x in xrange(9)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
max = 2000000
digits_list = []
for i in xrange(max + 1):
if i % 10000 in quads:
digits_list.append(i)
continue
if i % 1000 in trips:
digits_list.append(i)
continue
if i % 100 in dubs:
digits_list.append(i)
continue
print sum(digits_list)
This sums up a total of 180000 digits.
Problems:
>Adds quads as dubs and trips (if(dubs){}; if(trips){};)
>See pic related, does not include the 9* variant and also includes 0
>iterating with i+=1
>Values with more than 4 repeating digits would be counted e.g. 33333
Your solution is invalid
>Adds quads as dubs and trips (if(dubs){}; if(trips){};)
What?
>See pic related
No pic.
>iterating with i+=1
Where?
>Values with more than 4 repeating digits would be counted e.g. 33333
Oh, I see, so we don't want those? I wasn't aware.
That's an easy fix. give me a min.
Posting Pic *
>Your interpretation of what my loops were doing is fucking wrong
You didn't even post any code you noob
C'mon Sup Forums how can you be struggling like this?!?
>>iterating with i+=1
>Where?
for i in xrange(max + 1):
I don't think you understand what that does, xrange gives me an iterator, the value give to xrange is it's max calue.
docs.python.org
Also, this is a corrected code that doesnt count quads and higher:
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(size) for x in xrange(9)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
quints = digits(5)
max = 2000000
digits_list = []
for i in xrange(max + 1):
if i % 100000 in quints:
# skip repeating digits longer than 5
continue
if i % 10000 in quads:
digits_list.append(i)
continue
if i % 1000 in trips:
digits_list.append(i)
continue
if i % 100 in dubs:
digits_list.append(i)
continue
print sum(digits_list)
Forgot to say, if I skip quints(5 repeating) or higher, I get 179819920080.
>>Adds quads as dubs and trips (if(dubs){}; if(trips){};)
>What?
Didn't notice the continue in your code
Ohh, you are right, i missed that it didnt include 9.
But inclusion of 0 is correct.
Fixed:
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(size) for x in xrange(10)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
quints = digits(5)
max = 2000000
digits_list = []
for i in xrange(max + 1):
if i % 100000 in quints:
# skip repeating digits longer than 5
continue
if i % 10000 in quads:
digits_list.append(i)
continue
if i % 1000 in trips:
digits_list.append(i)
continue
if i % 100 in dubs:
digits_list.append(i)
continue
print sum(digits_list)
Now it's 199799900100
Phonecoding
#include
#include
int
digit(int n, int x)
{
const int p = pow10(n);
rtrn x % p / p;
}
int
repeat(const int n)
{
int i = 0;
int d = digit(1, n);
while (digit(++i, n) == d);
rtrn i;
}
int
sum(int n)
{
int s = 0;
int i, v;
for (i = 0; i < n; ++i) {
v = value(i);
if (v >= 2 && v
>I don't think you understand what that does, xrange gives me an iterator, the value give to xrange is it's max calue.
You iterate over every value in the range.
At least one person in this thread can (with help) get the correct result as shown in
>You iterate over every value in the range.
Hmmmm, oh, so that's what you were referring to.
Is that bad?
Are you saying I shouldn't be checking for dubs/quads/trips but generating them?
I guess that would be more efficient.
Maybe I can find a way to do that after dinner.
Thanks for clarifying the requirements for me and finding the bug in generation of digits to check.
print sum(x for x in range(2000000) if not (x%100%11 and x%1000%111 and x%10000%1111) and (x%100000%11111 and x%1000000%111111))
def digits(num, reps):
"""Takes a number, `num`, and checks to see if it has
`reps` repeating digits at the end."""
num = str(num)[-reps:]
for i in range(1,len(num)):
if num[i] != num[i-1]:
return False
return True
def main():
s = 0
for num in range(1,2000001):
for digits_type in range(2,5):
if digits(num, digits_type):
s += num
print(s)
if __name__ == '__main__':
main()
pretty sure this works but I haven't tested it
oh I might have added some numbers twice there with my nested for loops
I'm trying to generate the trips but I'm getting too much, I'm adding something I shouldn't be:
def mul(size):
return sum([pow(10, i) for i in xrange(size)])
def digits(size):
return [x * mul(size) for x in xrange(10)]
dubs = digits(2)
trips = digits(3)
quads = digits(4)
quints = digits(5)
def gen_em(max_val):
digits_list = set()
for i in xrange(100, max_val, 100):
digits_list |= set(
[i+x for x in dubs]
)
for i in xrange(1000, max_val, 1000):
digits_list |= set(
[i+x for x in trips]
)
for i in xrange(10000, max_val, 10000):
digits_list |= set(
[i+x for x in quads]
)
return list(digits_list)
Sum I'm getting is 199999899505
With Ada.Text_IO, Interfaces;
Use Ada.Text_IO, Interfaces;
procedure Sumgets is
Arg : Integer := 2_000_000;
Sum : Unsigned_64 := 0;
Count : Integer := 0;
subtype Get is Integer with Dynamic_Predicate => (Get mod 100 mod 11 = 0) or (Get mod 1000 mod 111 = 0) or (Get mod 10000 mod 1111 = 0);
subtype Exclude is Integer with Dynamic_Predicate => (Exclude mod 100000 mod 11111 = 0) or (Exclude mod 1000000 mod 111111 = 0);
begin
for i in 1 .. Arg loop
if i in Get and i not in Exclude then
Sum := Sum + Unsigned_64 (i);
Count := Count + 1;
end if;
end loop;
Put("Sum: " & Unsigned_64'Image(Sum));
New_Line;
Put(Integer'Image(Count) & " Occurences");
end Sumgets;
answer is 199,799,900,100
there are 199800 dubs, trips and quads
Why are you using ada?
Part of a university course?
i've seen it shilled here a few times and now i actually kind of like it
also what idiot university would teach Ada? its almost completely dead, even in the military/airplane industry