[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Sum all the diagonal values of a 1001x1001 clockwise integer

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 57
Thread images: 5

File: Screenshot_2017-04-10_19-25-16.png (23KB, 842x281px) Image search: [Google]
Screenshot_2017-04-10_19-25-16.png
23KB, 842x281px
Sum all the diagonal values of a 1001x1001 clockwise integer spiral or you're not a real programmer.
>>
current = 1
skip = 2
sum = 0
fizz = -1
while skip <= 1001
sum = sum + current
current = current + skip
fizz = fizz + 1
if fizz = 4 then
fizz = 0
skip = skip + 2
print sum
>>
>>59835051
     N/2-1
1 + ∑ 16x2 + 4x + 4
x=1

= 1 + 1/6 (4n3 + 3n2 + 8n - 15) + 1


def f(n): return (n*(n*(4*n+3)+8)-9)/6
print(f(1001))
>>
>>59835835
>668173001
Wrong, I'm afraid.
>>
>>59836115
Now someone do it in APL.
>>
I'm never going to get a job
Do you ever need to do shit like this in a "programming job"
>>
def diagsum(n):
result=1
for i in range(1,n//2+1):
result += 4*(2*i-1)**2+20*i
return result

Gives 669171001
>>
>>59836233
No, but this weeds out people who can't properly code.
>>
>>59836364
Math homework isn't coding.
>>
>>59835051
>Sum all the diagonal values of a 1001x1001 clockwise integer spiral or you're not a real programmer.

I guess OP is not a real programmer.

Have you thought about a career in maintenance?
>>
>>59836380
>Math
This doesn't require math, just logic, since you can generalize the spiral and brute force a solution. E.g. there are 4 corners, and for each clockwise rotation of the spiral, the step to the next increases by 2. If you can't figure that out by looking at the example, you're worthless as a programmer. Anyone who can't figure out trivial logic is.
>>59836505
Here:
spiral = tuple(range(1, 1001*1001 + 1))
SUM = 0
corner = count = 0
increment = 2
end = max(spiral) - 1
while corner <= end:
SUM += spiral[corner]
corner += increment
count += 1
if count == 4:
count = 0
increment += 2
print(SUM)

$ time python3 euler_28.py
669171001

real 0m0.167s
user 0m0.144s
sys 0m0.024s
>>
For any given NxN spiral where N is an odd number, the value in the upper right corner is N^2. We can deduce the values in the remaining squares going counter clockwise by subtracting N-1 from this value three times, and recording those values each time. Accordingly, we can write a program that calculates the spiral sum as such:

(define (spiral-sum dim)
(define (corner-sum n)
(let ((corner (* n n))
(decrement (- n 1)))
(+ corner
(- corner decrement)
(- corner (* 2 decrement))
(- corner (* 3 decrement)))))
(if (= dim 1)
1
(+ (corner-sum dim) (spiral-sum (- dim 2)))))


And running the program returns:
> (spiral-sum 1001)
669171001
>>
>>59835051
5343342001
>>
>>59835051
NIGHTMARE MODE

Now perform the same procedure for a 1001x1001 prime spiral

73 79 83 89 97
71 17 19 23 29
67 13 2 3 31
61 11 7 5 37
59 53 47 43 41
>>
>>59837100
1 is a prime.
>>
>>59837111
Nope 1 is special. Its the identity element
>>
>>59837100
>primespiral.py
9882713664
0.100000143051 seconds


Is it correct?
>>
>>59837272
I'm just going to assume yes, because it's accurate for smaller sizes against manual checking.

I basically just took this code >>59836576 and applied a couple changes
import primesieve #If you don't have it: pip install primesieve

spiralsize = 1001*1001

spiral = primesieve.n_primes(spiralsize)
SUM = 0
corner = count = 0
increment = 2
end = spiralsize-1
while corner <= end:
SUM += spiral[corner]
corner += increment
count += 1
if count == 4:
count = 0
increment += 2
print SUM
>>
>>59837272
Good job

ASCENDED MODE

Consider the infinite spiral grid of integers.
Using zeta function regularisation , sum all the diagonals.
>>
>>59836576
You solved the logic but still allocate the entire spiral/field.
Isn't the point to either generatie the entire spiral and ''brute force'' it OR generalise the logic and make it efficient
>>
if you can't do this in your head you're not a real programmer
>>
>>59837798
if you don't know the answer from a past life, you're not a real programmer
>>
>>59836576
>No math required
I solved it with a recurrence relation.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>

int64_t T(int64_t n){
if(n == 0){
return 1;
}else{
return T(n-1) + 2 + 2*((n-1)/4);
}
}

int main(int argc, const char * argv[]) {
int64_t sum = 0;
int I = 0;
for( I = 0; I < 2001;I++){
sum+=T(I);
}
printf("Sum = %ld\n",sum);
return 0;
}
>>
It can be done in O(1). Consider a k > 1 th layer in an n x n spiral (n must be odd)

The corners are k^2 + (k^2 - (k-1)) + (k^2 - 2(k-1) ) + (k^2 - 3(k-1) ) = 4 k ^2 - 6k + 6

k ranges in all the odd numbers from 3, 5 ... n
Including the first layer we just need to compute the sum

1 + \sum_{ k <= n , k \in 2 Z - 1 } (4 k ^2 - 6k + 6)

This sum can be re-written as a formula

 25 + 2/3 Floor[
1/2 (-3 + n)] (67 +
Floor[1/2 (-3 + n)] (39 + 8 Floor[1/2 (-3 + n)]))
>>
>>59838004
which is indeed
1/6 (-9 + n (8 + n (3 + 4 n)))
as n is odd
>>59836115
>>
If you bother solving this kind of problem you're not a real programmer. Seriously.
>>
>>59838039
>Im too much of a brainlet to do basic maths
>>
>>59838067
>does basic math for /g/
>>
>>59838075
>cant handle a fun problem solving exercise
>too dumb to solve it with basic logic
>waaah not a real programmerrr!!
>haha you post on /g/
Classic
>>
>>59838096
>basic math
>fun
You're very easily entertained anon. I applaud you for that. I suppose my claim was a bit broad.
>>
>>59835051
section .text 
_start:
mov edx, 1
lea ebx, [2*edx]
mov eax, edx
.l:
mov cl, 4
.a:
add edx, ebx
add eax, edx
loop .a
add ebx, 2
cmp ebx, 1002
jb .l
printd eax
>>
>>59835051
Every new(4 steps) outer layer adds 2 to the iteration. 1 --> 3 --> 5 --> 7 --> 9 ----> 13 ----> 17 ----> 21 ----> 25 etc.

I can also see patterns by multiplying the primes in the previous layer to get the numbers of the current layer but that's a longshot. I can't figure out that pattern completely.
>>
>>59838110
>math
Its maths kiddo

Basic problems can still be fun.
>>
Messed up the logic in my deleted post

#include <iostream>

int main(){
int sum = 1;
int inc = 1;
for(int layer = 0; layer < (1001-1)/2; layer++){
for(int i = 0; i < 4; i++){
inc += 2 * (layer + 1);
sum += inc;
}
}
std::cout << "Sum: " << sum << std::endl;
return 0;
}
>>
Honestly all these brainlets who cant do it in O(1) why do you bother?
>>
>>59838450
int main(){
return 669171001;
}


Like this?
>>
>>59835051
Just step through the lines and Start from 0 on one int and len on the other int with a check for the same so you don't add the middle number twice.
>>
>>59835051
>doing your project euler for you
Fuck off
>>
Came up with two solutions and they're both shitty. Oh well.

function F(size) {
const n = Math.floor(size / 2);
let result = 0;
for (let i=2;i<=8;i+=2) {
let previous = 1;
for (let j=0;j<n;++j) {
let delta = i + 8 * j;
previous += delta;
result += previous;
}
}
return result + 1;
}


function F(size) {
const end = Math.pow(size, 2);
let [ last, distance, left, result ] = [ 1, 0, 1, 0 ];
for (let i=1;i<=end;++i) {
if (i - last !== distance) continue;
[ result, last ] = [ result + i, i ];
if (--left > 0) continue;
[ left, distance ] = [ 4, distance + 2 ];
}
return result;
}
>>
>>59838001
why not (n-1) / 2 instead of 2 * (( n - 1 ) / 4)?
>>
>>59838757
Codemonklets are dumb
>>
def sum_diag(size):
sum = 1

for side_length in range(3, size+1, 2):
res = (4 * side_length**2) - (6 * (side_length - 1))
sum += res

return sum
>>
>>59835051
Here's your homework, OP. I'm sure there's a more elegant way but it works.
>>
>>59838757
the recurrence relation I came up with is
T(0) = 1
T(n) = T(n-1) + n + n*floor((n-1)/4)

In this form you can clearly see that the corners increase by two every four terms in the sequence.

When working with integers we get the floor for free so I could have simplified,like you suggest but it would have made the recurrence relation harder to read and understand. Additionally I could unroll the recurrence relation and get a constant time result if I wanted, but fuck that.

>>59839035
Heres your (you) faggot.
>>
>>59839544
T(n) = T(n-1) + 2 + 2*floor((n-1)/4) *
>>
>>59836124
It's correct.
>>
File: spiral.png (12KB, 661x418px) Image search: [Google]
spiral.png
12KB, 661x418px
Could be right.
>>
File: spiral.png (11KB, 661x418px) Image search: [Google]
spiral.png
11KB, 661x418px
>>59840255
Nope, now it's right.
>>
File: spiral.png (14KB, 661x418px) Image search: [Google]
spiral.png
14KB, 661x418px
>>59840255
>>59840448
Now I am confused but the first one was definitely correct. Outputting the value of $last at the start of the central for loop creates the correct pattern of increase right up to the end (each integer in the last set will be 1000 higher than the last).
>>
Enough of this basic bitch shit

Write a program to print an odd d x d spiral.
>>
This is actually easy by hackerrank standards. You all should join, is pretty good, honestly.
>>
>>59841408
>Have to manually parse input
Hackerrank sucks bro.
>>
>>59838450
> Project Euler
> Why bother writing a program
>>
>>59835051
I want to write this up with trigonometric functions, but nah, it's too much work for no reward.
>>
>>59835051

Sum((I+I').×[Matrix])-1

Ya im useing Matlab.....im lazy and typing code on phone sucks

Python is probably very simmilar
>>
unsigned long long int do_the_shit(int n)
{
if (n % 2 == 0) n--; // assume the user is a nigger and correct him
unsigned long long int sum = 1;
for (int i = n; i > 1; i -= 2)
sum += (4*i*i - 6*i + 6);
return sum;
}

rate me
how could i make this better?
>>
>>59843989
unsigned long long int do_the_shit(int n)
{
n = n / 2 + 1;
return (16*n*n*n - 18*n*n + 14*n - 9) / 3;
}
Thread posts: 57
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.