Delegates

Discussion of challenges you have already solved
chrjue
Posts: 12
Joined: Fri Oct 31, 2008 3:23 pm
Contact:

Delegates

Post by chrjue »

Thought about coding first. But then i found a faster solution using excel: I mad two lists, first one with numbers from 1 to 2118, second one with the squares of the first list up to the result 2116. Then i just summed all the values up, and there it was...
thalion
Posts: 1
Joined: Fri Oct 31, 2008 10:49 pm

Post by thalion »

Hm don't think excel is much faster. Took me about 5 mins to code that in c++.
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

It's a one-liner in Python, and still quite readable, if somewhat inefficient:

Code: Select all

>>> print sum(xrange(2119)) + sum([x*x for x in xrange(2119) if x*x < 2119])
2277532
Or you can use xrange(1+int(2119**0.5)) instead, and get rid of the conditional.

I'm really finding Python well-suited for these numerical challenges. This one's pretty basic, but it seems to scale well to bigger problems (the high fibs for example).
chrjue
Posts: 12
Joined: Fri Oct 31, 2008 3:23 pm
Contact:

Post by chrjue »

started learning python instantly, even if it just took me < 2 minutes in excel ;-)
syncbit
Posts: 1
Joined: Mon Nov 17, 2008 6:24 pm

Post by syncbit »

a bit mathematics will do the trick to:

sum of 1 + 2 + .. + n = n(n+1)/2

sum of 1^2 + 2^2 + 3^2 + ... n^2 = n(n+1)(2n+1)/6

sqrt(2119) = 46

so the answer is: 2118*2119/2 + 46*47*93/6
DocJoe
Posts: 5
Joined: Sun Nov 02, 2008 1:22 pm
Location: Vagina Hole

Post by DocJoe »

syncbit,

great!

I remembered the (n+1)*(n/2) from school but not the one to calc the sum of the squares.

Thanks,

DJ
bottomy
Posts: 4
Joined: Sat Jun 13, 2009 5:53 pm

Post by bottomy »

i solved it by using my C program i made, but cause i didn't program it properly i could only check the value of (numbers ending in 0) 2110 then i had to add manually the rest of the numbers to 2118 then added manually the square numbers.

can someone help me fix my code so i can workout other numbers besides those ending with 0.

Code: Select all

#include <stdio.h>


main ()               

{ int i;
	
	printf("enter number: ");
	scanf("%d",&i);
	
	
	{
		printf ("%f",i * (i / 10 * 5 + 0.5));
	}
	printf ("\n");
	
}

the equation i made works on a calc, but not here. i think its because after it divides the number by ten if its not a whole number then it won't workout properly. so yeh can anyone fix up my code :P. btw i just started learning C earlier today XD (so i am very noobie :P).
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

Yes, integer division rounds. But if you write 10.0 explicitly then it will force it to use double-precision floating point instead.

But dividing by 10 and then multiplying by five, without rounding, is the same as dividing by two.

You've picked C up pretty quickly! But some of your syntax is odd or outdated. Instead of main() you ought to write "int main()" - the default return type is int anyway, but relying on the default is outdated. You should explicitly return an integer at the end of the function, even if it's just zero. If you don't want to return a value from a function, declare it as returning type "void", but you're not allowed to do that for the main function because the library code that calls it expects it to return something.

More aesthetically, the braces around the inner printf don't achieve anything - you can take them out.
bottomy
Posts: 4
Joined: Sat Jun 13, 2009 5:53 pm

Post by bottomy »

ah, thanks :D
xulfer
Posts: 5
Joined: Mon Nov 02, 2009 11:27 am

Post by xulfer »

Hadn't solved anything using NASM yet so I figured I'd go ahead and have a little fun on this one.

compile:
nasm -felf32 sourcefile.asm
ld -o delegate sourcefile.o
or on x86_64 systems
ld -m elf_i386 -o delegate sourcefile.o

Code: Select all

section .data
rprefix:    db "Result: "
rplen: equ $-rprefix
eol:        db 10
psquare:    dw 1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,1024,1089,1156,1225,1296,1369,1444,1521,1600,1681,1764,1849,1936,2025,2116,0
base:       dd 10
maxnum:     equ 2118

section .text
            global _start
            global main

_start:
            mov     eax, 0                  ; result
            mov     ebx, 0                  ; iterator
.add:
            inc     ebx                     ; increment the iterator
            cmp     ebx, maxnum
            ja      .done                   ; operation completed
            add     eax, ebx                ; add to sum
.tchk:
            mov     edx, 0                  ; start at 0
.tloop:
            movzx   ecx, word[psquare+edx]  ; pull in our table entry
            cmp     ecx, 0
            je      .add                    ; reached end of table            
            cmp     ecx, ebx                ; see if we have a perfect square
            je      .addagain               ; perfect square found add again
            ja      .add
            add     edx, 2                  ; increment our table iterator
            jmp     .tloop                  ; keep looping
.done:
            push    eax                     ; temporarily store eax
            mov     eax, 4                  ; call write() syscall
            mov     ebx, 1                  ; stdout
            mov     ecx, rprefix            ; point to result string
            mov     edx, rplen              ; string length
            int     80h
            pop     eax                     ; load our sum into eax
            call    .udot                   ; print the integer
            mov     eax, 4                  ; write() syscall
            mov     ebx, 1                  ; stdout
            mov     ecx, eol                ; eol character
            mov     edx, 1                  ; length is 1
            int     80h
            mov     eax, 1                  ; exit
            mov     ebx, 0
            int     80h
            ret

.addagain:
            add     eax, ebx                ; add again
            jmp     .add                    ; restart loop

.udot:  
            xor     edx,   edx
            div     dword[base]             ; edx: num mod radix
            sub     dl,    10               ; convert remainder to ascii
            sbb     cl,    cl               ; hex correction for digits >9
            add     dl,    'A'              ; (avoiding conditional jumps)
            and     cl,    '0'+10-'A'       ; gap between ASCIIs of 9 and A
            add     dl,    cl               ; binary > ascii
            push    edx                     ; push ascii
            or      eax,   eax              ; all digits done ?
            je      .outchr                 ; yes, jump to output
            call    .udot                   ; no, next digit
.outchr: 
            mov     ecx,   esp              ; string address
            mov     edx,   1                ; string len
            mov     eax,   4                ; write syscall
            mov     ebx,   1                ; filehandle
            int     080h                    ; output character
            pop     eax                     ; trash character
            ret
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

Great to see some "real" programmers in this forum :lol:
There are far to few challenges dealing with assembler. In fact I remember only one where it was necessary to know x86 assembler. I'd never think of solving this one in assembler... anyway, great job, man!
xulfer
Posts: 5
Joined: Mon Nov 02, 2009 11:27 am

Post by xulfer »

Thanks a lot. Nasm is something that I always have a lot of fun doing things in. Hopefully I can find other challenges that could make use of it soon.
Aghamemnon
Posts: 49
Joined: Fri Jul 02, 2010 9:34 pm
Location: Egypt
Contact:

Post by Aghamemnon »

In my school they didn't learn us to count the sum of squares
but I generated this -->(x+1)*(x/2)
Didn't know the sum of squares
thanks syncbit
:twisted: Devilish Angel Aghamemnon :twisted:
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

You can derive the sum of squares expression from the n(n+1)/2 identity:

1) Expand out sum<1,n>(2x-1) and you'll see that it's equal to n^2

2) Hence the sum of the first n squares has n 1s, n-1 3s, n-2 5s, and so on - so it's sum<1,n>((n-x+1)(2x-1))

3) Expand that out into separate sums and solve for sum<1,n>(x^2) (which now appears on both sides)

I find solutions like this interesting because the steps aren't blindingly obvious, but you can work them out through intuition - you start by guessing the answer, then prove it. There's also an element of faith along the way that the steps you're taking will lead to a solution. It's similar to the proof of n(n+1)/2 - there's an intuitive trick you need to apply.
error213
Posts: 4
Joined: Wed Oct 15, 2008 12:00 pm

Post by error213 »

solution in C#

Code: Select all

static void Main(string[] args)
        {
            int Hillary = 0;
            for (int Barack = 1; Barack <= 2118; Barack++)
            {
                if ((int)Math.Sqrt(Barack) == Math.Sqrt(Barack))
                {
                    Hillary += Barack;
                    Hillary += Barack;
                }
                else
                    Hillary += Barack;
            }
            Console.WriteLine(Hillary);
        }
Post Reply