Delegates
Delegates
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...
It's a one-liner in Python, and still quite readable, if somewhat inefficient:
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).
Code: Select all
>>> print sum(xrange(2119)) + sum([x*x for x in xrange(2119) if x*x < 2119])
2277532
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).
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.
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 . btw i just started learning C earlier today XD (so i am very noobie ).
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 . btw i just started learning C earlier today XD (so i am very noobie ).
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.
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.
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
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
-
- Posts: 49
- Joined: Fri Jul 02, 2010 9:34 pm
- Location: Egypt
- Contact:
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.
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.
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);
}