Jump to content
whackatre

Programming Challenge - SUM

Recommended Posts

This is a super easy assignment. The goal is: how many lines can you reduce your solution to (hint: functional programming)?

Rule: Each statement must go on its own line. For example, if you're using Java, you cannot do hi();hi();hi();hi();hi(); and say you only used one line.

Task: Print the sum of all the multiples of thirteen or nineteen up to 690,690.

My code in Ruby:
[CODE]
(1...690690).select {|x| x % 13 == 0 or x % 19 == 0}.inject {|sum, x| sum + x}[/CODE]

If the number ends with 55, you're probably correct.

Have fun! Edited by whackatre

Share this post


Link to post
Share on other sites
I'm starting to love Python:

[code]reduce(lambda s,n: s+n, [i for i in range(1, 690690) if i % 13 is 0 or i % 19 is 0])[/code]

edit; this does also work:

[code]sum([i for i in range(1, 690690) if i % 13 is 0 or i % 19 is 0])[/code]

edit 2; moar!

[code]sum(filter(lambda i: not (i % 13 and i % 19), range(1, 690690)))[/code]

[code]sum([i for i in range(1, 690690) if not (i % 13 and i % 19)])[/code] Edited by Faab234

Share this post


Link to post
Share on other sites
C++

[code]#include <iostream>

using namespace std;

void main()
{
int value = 0;

for (int i = 0; i < 690690; i++)
{
if (i % 13 == 0 || i % 19 == 0)
{
value += i;
cout << "Value: " << i << endl;
}
}
}[/code]

Pretty similar in Java.

Share this post


Link to post
Share on other sites
[quote name='Hash']C++

[code]#include <iostream>

using namespace std;

void main()
{
int value = 0;

for (int i = 0; i < 690690; i++)
{
if (i % 13 == 0 || i % 19 == 0)
{
value += i;
cout << "Value: " << i << endl;
}
}
}[/code]

Pretty similar in Java.[/QUOTE]

I stopped at "void main".

Share this post


Link to post
Share on other sites
[quote name='Trey']I stopped at "void main".[/QUOTE]

Could've added:

[code]system("pause");[/code]

when it comes to the end.

Share this post


Link to post
Share on other sites
[quote name='Hash']Could've added:

[code]system("pause");[/code]

when it comes to the end.[/QUOTE]

I would have shot you

Share this post


Link to post
Share on other sites
[quote name='Trey']I stopped at "void main".[/QUOTE]
[MENTION=17381]Hash[/MENTION] shouldn't it be this:

[code]#include <iostream>

using namespace std;

int main()
{
int value = 0;

for (int i = 0; i < 690690; i++)
{
if (i % 13 == 0 || i % 19 == 0)
{
value += i;
cout << "Value: " << i << endl;
}
}
return 0;
}[/code]

Share this post


Link to post
Share on other sites
[quote name='Faab234'][MENTION=17381]Hash[/MENTION] shouldn't it be this:

[code]#include <iostream>

using namespace std;

int main()
{
int value = 0;

for (int i = 0; i < 690690; i++)
{
if (i % 13 == 0 || i % 19 == 0)
{
value += i;
cout << "Value: " << i << endl;
}
}
return 0;
}[/code][/QUOTE]

Yea, I'm taking bad habits from Java since I started learning C++ 1 week ago.

Share this post


Link to post
Share on other sites
[quote name='Faab234'][MENTION=17381]Hash[/MENTION] shouldn't it be this:

[code]#include <iostream>

using namespace std;

int main()
{
int value = 0;

for (int i = 0; i < 690690; i++)
{
if (i % 13 == 0 || i % 19 == 0)
{
value += i;
cout << "Value: " << i << endl;
}
}
return 0;
}[/code][/QUOTE]

Anything other than int main is non-standard and bad. It's not illegal, just bad practice.

Share this post


Link to post
Share on other sites
Here's one in haskell:

[code]
import Data.List

main = print (sum [x | x <- [1 .. 690690], x `mod` 19 == 0 || x `mod` 13 == 0])
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×