September 23, 2024, 08:22:38 PM

1,531,321 Posts in 46,731 Topics by 1,523 Members
› View the most recent posts on the forum.


I decided to step up to Guff's challenge

Started by ncba93ivyase, January 19, 2009, 11:18:13 PM

previous topic - next topic

0 Members and 1 Guest are viewing this topic.

Go Down

Daddy


ncba93ivyase

Quote from: guff on January 25, 2009, 10:02:54 AM
uh that gives the wrong answer doodhuh;

you're assuming that the asnwer will be a perfect square (hint: it isn't)  akudood;
oh goddamnit :'(

I'll work on the true solution

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

ncba93ivyase

Code Select
"""A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.

Find the largest palindrome made from the product of two 3-digit numbers."""

y = []
for i in range(100, 1000):
for o in range(100, 1000):
x = str(i*o)
if x == x[::-1]:
y.append(int(x))
print max(y)

That should be right... I hope.

But I really, really hate the way I did it and it's also really slow, so I'm going to rework it for a better solution.

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

Daddy

Quote from: Pancake Persona on January 26, 2009, 12:05:22 AM
oh goddamnit :'(

I'll work on the true solution
You didn't check if you had the right answer before posting the code?  akudood;

and yeah that one works.

guff

QuoteJMV290
AIM
10:37
something is werid.
10:37
weird ;_;

Commodore Guff
AIM
10:37
how so

JMV290
AIM
10:37
/*Find the sum of all the multiples of 3 or 5 below 1000.*/
package euler_001;

/**
*
* @author jamesvalente
*/
public class Euler001 {

    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i < 1000; i++) {
            if (3 % i == 0 || 5 % i == 0) {
                sum += i;
            }
        }
        System.out.println(sum);
    }
}
10:37
That should work
10:38
but it gives me 9
10:38
solution someone gave for the problem after i gave up and looked
10:38
public class mathschallenge
{
public static void main(String[] args)
{
int total = 0;
for(int i = 1; i < 1000; i ++)
{
if(i % 3 == 0 || i % 5 == 0)
total += i;
}
System.out.println(total);
}
}
10:38
same code except variable names
10:39
and the braces on the if statement don't change anything, they just make it more readable

Commodore Guff
AIM
10:40
dur
10:40
jamie
10:40
you have a serious problem with ordering things
10:41
the problem is in your if statement
10:41
3%i is the remainder you get when dividing 3 by i

JMV290
AIM
10:41
oh i forgot perentsitis

Commodore Guff
AIM
10:41
that's not what you want
10:41
NO

JMV290
AIM
10:41
lol

Commodore Guff
AIM
10:41
THAT'S NOT THE ISSUE
10:41
jamie
10:41
i%3 and i%5 are what you want
10:41
not 3%i and 5%i

JMV290
AIM
10:42
oh shit DYSLEXIA

Commodore Guff
AIM
10:42
:|

JMV290
AIM
10:42
forget this ever hapend THIS IS WHAT HAPPENS WHEN I EAT NO FOOD FOR HOURS

Commodore Guff
AIM
10:43
i save all my im logs

JMV290
AIM
10:43
IT NEVER AHPPENED GUFF


n_u

Daddy


Daddy

January 26, 2009, 09:24:39 AM #66 Last Edit: January 26, 2009, 09:43:25 AM by JMV
PHP
Code Select
<?php /** Find the sum of all the multiples of 3 or 5 below 1000.*/
$sum 0;
for(
$i=1;$i<1000;$i++){
if(
$i == || $i == 0){
$sum+=$i;}}
echo 
$sum;
?>


Java
Code Select
/*Find the sum of all the multiples of 3 or 5 below 1000.*/
package euler_001;
/* @author jamesvalente*/
public class Euler001 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i < 1000; i++) {
            if (i % 3 == 0 || i % 5 == 0) {
                sum += i;
            }
        }
        System.out.println(sum);
    }
}


Python
Code Select
#!/usr/bin/python
# Filename : Euler_001.py
# Find the sum of all the multiples of 3 or 5 below 1000.
print sum(range(3, 1000, 3)) +sum(range(5, 1000, 5)) - sum(range(15, 1000, 15))


guff

Code Select
/* Problem 1:
Find the sum of all the multiples of 3 or 5 below 1000.
*/

#include <stdio.h>

int main() {
int sum = 0;
int i;
for(i = 0; i < 1000; i++) {
if((i % 3 == 0) || (i % 5 == 0)) {
sum += i;
}
}
printf("%d\n", sum);
return(0);
}


i hate c i hope it dies  madood;

Daddy

Quote from: guff on January 26, 2009, 09:39:47 AM
Code Select
/* Problem 1:
Find the sum of all the multiples of 3 or 5 below 1000.
*/

#include <stdio.h>

int main() {
int sum = 0;
int i;
for(i = 0; i < 1000; i++) {
if((i % 3 == 0) || (i % 5 == 0)) {
sum += i;
}
}
printf("%d\n", sum);
return(0);
}


i hate c i hope it dies  madood;
it looks like java.

speaking of java, brb removing multilne comments I don't need.  akudood;

Daddy

And, in Javascript.  baddood;
Code Select
<script type="text/javascript">
//Find the sum of all the multiples of 3 or 5 below 1000.
var sum=0;
for(var i=0;i<1000;i++){
if(i%3==0 || i%5==0){
sum+=i;
}
}
document.write(sum);
</script>


Gladjaframpf

PLT Scheme:
Code Select

(display (foldr + 0 (build-list 1000 (lambda (x) (if (or (= (modulo x 3) 0) (= (modulo x 5) 0)) x 0)))))


Displayed on one line for maximum unreadability. akudood;


MIPS Assembly:
Code Select

lis $1
.word 0
lis $2
.word 1
lis $3
.word 1
lis $4
.word 3
lis $5
.word 5
lis $6
.word 1000

loop:
div $2, $4
mfhi $7
beq $7, $0, 3
div $2, $5
mfhi $7
bne $7, $0, 1
add $1, $1, $2

add $2, $2, $3
slt $7, $2, $6
bne $7, $0, loop

jr $31


This doesn't actually print the result, it just stores it in register $1, but I didn't want to stick the print routine in because it's big and ugly and my MIPS emulator automatically prints the contents of all the registers when it's done processing anyways.

Daddy

I need to find out how to do loops in Clojure.  madood; madood; madood;

ncba93ivyase

whoa, i didn't even notice guff did number 1 in c

Because that's what I just did:
Code Select
#include <stdio.h>
main(){
int i, x;
i = 0;
x = 0;
while (i<1000){
if (i % 5 == 0 || i % 3 == 0)
x+=i;
i++;
}
printf("%d \n\n", x);
}


i kept getting the wrong answer with this for a good twenty minutes and it was driving me nuts and then finally asked jmv. the second i hit my return key, i noticed i forgot to define x goonish

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Code Select
sum [i|i<-[1..999],i`mod`3==0||i`mod`5==0]

haskell akudood;
i could probably shorten it a bit using the same trick i did for my python solution but this was easier to write in ten seconds because i didn't have to look anything up

Code Select
+/((-.*5|>:i.1000) +. (-.*3|>:i.1000))#>:i.1000

akudood;

i haven't looked at j for a while now and after seeing this code again i remember why
my code for this is horribly innefficient; ideally i should only have to call i.1000 once but using the stuff i knew this was the only way i could think of at the time baddood;

guff

Quote from: guff on January 26, 2009, 02:39:00 PM
Code Select
sum [i|i<-[1..999],i`mod`3==0||i`mod`5==0]
okay shortened it to:
Code Select
sum [i|i<-[1..999],0`elem`[i`mod`3,i`mod`5]]

and by shorter i mean i made it a little longer but it would be shorter if shit damn haskell used shit damn % for modulo  akudood;

Go Up