Project Euler: Level 1

Warm up lesson before going into deep..

Problem #1:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

ย 

C: main.c
#include "test_sum_of_multiplies_impl.h"

int getSumOfMultiplies(const int N)
{
  int ret = 0;
  if (0 == N)
    return ret;
  int i = 1;
  for (; i < N; i++)
    ret += ((i % 3 == 0) || (i % 5 == 0)) ? i : 0;
  return ret;
}

void main()
{
  test_getSumOfMultiplies(-1, &getSumOfMultiplies);
  test_getSumOfMultiplies(0, &getSumOfMultiplies);
  test_getSumOfMultiplies(1, &getSumOfMultiplies);
  test_getSumOfMultiplies(2, &getSumOfMultiplies);
  test_getSumOfMultiplies(3, &getSumOfMultiplies);
  test_getSumOfMultiplies(4, &getSumOfMultiplies);
}
test_sum_of_multiplies_impl.h
#ifndef TEST_SUM_OF_MULTIPLIES_IMPL_H
#define TEST_SUM_OF_MULTIPLIES_IMPL_H

#include 
#include 

#define PASS          "PASSED"
#define FAIL          "FAILED"
#define MAX_FUNC_NAME 254

char* test_func_name[MAX_FUNC_NAME];
int(*test_func)(int) = NULL;

int test_zero()
{
  strcpy_s(test_func_name, (MAX_FUNC_NAME * sizeof(char)), __FUNCTION__);
  return (test_func(0) == 0) ? 1 : 0;
}
int test_below_10()
{
  strcpy_s(test_func_name, (MAX_FUNC_NAME * sizeof(char)), __FUNCTION__);
  return (test_func(10) == 23) ? 1 : 0;
}
int test_below_1000()
{
  strcpy_s(test_func_name, (MAX_FUNC_NAME * sizeof(char)), __FUNCTION__);
  return (test_func(1000) == 233168) ? 1 : 0;
}
int test_below_1001()
{
  strcpy_s(test_func_name, (MAX_FUNC_NAME * sizeof(char)), __FUNCTION__);
  return (test_func(1001) == 234168) ? 1 : 0;
}

// Test 'getSumOfMultiplies' function for specific value
//[in] testType:
//     0 - zero
//     1 - ten
//     2 - thousand
//     3 - thousand and one
//[in] testFunc:
//     Function to be tested:
//     int testFunc ( int maxValue )
void test_getSumOfMultiplies(const int testType, const void* testFunc)
{
  int res = 0;
  test_func_name[0] = 0;
  test_func = testFunc;
  printf("running test:      \t\t");
  switch (testType)
  {
  case 0:
    res = test_zero();
    printf("%s\n", test_func_name);
    break;
  case 1:
    res = test_below_10();
    printf("%s\n", test_func_name);
    break;
  case 2:
    res = test_below_1000();
    printf("%s\n", test_func_name);
    break;
  case 3:
    res = test_below_1001();
    printf("%s\n", test_func_name);
    break;
  default:
    printf("error: invalid test type '%d'\n\n", testType);
    return;
  }
  if (1 == res)
    printf("result: \t\t\t%s\n\n", PASS);
  else
    printf("result: \t\t\t%s\n\n", FAIL);
}
#endif // !TEST_SUM_OF_MULTIPLIES_IMPL_H
C++: main.cpp
#include "unittest.hpp"

int getSumOfMultiplies(const int N)
{
  int ret = 0;
  if (0 == N)
    return ret;
  for (int i = 0; i < N; i++)
    ret += ((i % 3 == 0) || (i % 5 == 0)) ? i : 0;
  return ret;
}

int main()
{
  UnitTest ut(getSumOfMultiplies);
  ut.run(-1);
  ut.run(0);
  ut.run(1);
  ut.run(2);
  ut.run(3);
  ut.run(4);
}
unittest.hpp
#ifndef UNITTEST_HPP
#define UNITTEST_HPP

#include 
#include 

class UnitTest
{
public:
  //[in] testFunc:
  //     Function address to be tested:
  //     int testFunc ( int maxValue )
  UnitTest(const void* funcAddr);

  void printResult(const int& testType);

  // Test 'getSumOfMultiplies' function for specific value
  //[in] testType:
  //     0 - zero
  //     1 - ten
  //     2 - thousand
  //     3 - thousand and one
  void run(const int& testType);

private:
  int test_zero();
  int test_below_10();
  int test_below_1000();
  int test_below_1001();

  int(*m_testFunc)(int);
  std::string m_funcName;
};

#endif // !UNITTEST_HPP

unittest.cpp
#include "unittest.hpp"

const std::string& PASS{ "PASS" };
const std::string& FAIL{ "FAIL" };

UnitTest::UnitTest(const void* funcAddr)
{
  m_testFunc = (int(*)(int))funcAddr;
}

void UnitTest::run(const int & testType)
{
  int res = 0;
  std::cout << "running test:      \t\t";
  printResult(testType);
}

void UnitTest::printResult(const int & testType)
{
  int res = 0;
  switch (testType)
  {
  case 0:
    res = test_zero();
    break;
  case 1:
    res = test_below_10();
    break;
  case 2:
    res = test_below_1000();
    break;
  case 3:
    res = test_below_1001();
    break;
  default:
    std::cout << "error: invalid test type '" << testType << "'\n\n";
    return;
  }
  std::cout << m_funcName << std::endl;
  if (1 == res)
    std::cout << "result: \t\t\t" << PASS << "\n\n";
  else
    std::cout << "result: \t\t\t" << FAIL << "\n\n";
}

int UnitTest::test_zero()
{
  m_funcName = __func__;
  return (m_testFunc(0) == 0) ? 1 : 0;
}

int UnitTest::test_below_10()
{
  m_funcName = __func__;
  return (m_testFunc(10) == 23) ? 1 : 0;
}

int UnitTest::test_below_1000()
{
  m_funcName = __func__;
  return (m_testFunc(1000) == 233168) ? 1 : 0;
}

int UnitTest::test_below_1001()
{
  m_funcName = __func__;
  return (m_testFunc(1001) == 234168) ? 1 : 0;
}
Java:

public class TestGetSumOfMultiplies
{
  public static void main(String[] args)
  {
    UnitTest ut = new UnitTest();
    ut.RunTests();
  }
  
  public static int GetSumOfMultiplies(int N)
  {
    int ret = 0;
    if (0 == N)
      return ret;
    for (int i = 0; i < N; i++)
      ret += ((i % 3 == 0) || (i % 5 == 0)) ? i : 0;
    return ret;
  }
}


public class UnitTest
{
  public void RunTests()
  {
    for(int i=0; i<4; i++)
    {
      System.out.print("running test:      \t\t");
      switch (i)
      {
        case 0:
        TestZero();
        System.out.println(funcName);
        System.out.println("result: \t\t\t" + result + "\n"); 
        continue;
        case 1:
        TestBelow10();
        System.out.println(funcName);
        System.out.println("result: \t\t\t" + result + "\n"); 
        continue;
        case 2:
        TestBelow1000();
        System.out.println(funcName);
        System.out.println("result: \t\t\t" + result + "\n"); 
        continue;
        case 3:
        TestBelow1001();
        System.out.println(funcName);
        System.out.println("result: \t\t\t" + result + "\n"); 
        continue;
        default:
        System.out.println("error: invalid test type..");
        break;
      }
    }
  }
  
  private String funcName;
  private String result;                           
  
  private void TestZero()
  {
    funcName = "TestZero";
    result = (TestGetSumOfMultiplies.GetSumOfMultiplies(0) == 0) ? "PASSED" : "FAILED";
  }

  private void TestBelow10()
  {
    funcName = "TestBelow10";
    result = (TestGetSumOfMultiplies.GetSumOfMultiplies(0) == 0) ? "PASSED" : "FAILED";
  }

  private void TestBelow1000()
  {
    funcName = "TestBelow1000";
    result = (TestGetSumOfMultiplies.GetSumOfMultiplies(0) == 0) ? "PASSED" : "FAILED";
  }

  private void TestBelow1001()
  {
    funcName = "TestBelow1001";
    result = (TestGetSumOfMultiplies.GetSumOfMultiplies(0) == 0) ? "PASSED" : "FAILED";
  }
}

Python:

JavaScript:

Go:

Haskell:

Advertisements

2 thoughts on “Project Euler: Level 1

    1. Hi BluePajamas!
      It’s nice to hear that. To be honest I totally forgot to update my blog with project Euler problems due to that I’m doing these days sooo many things: basketball, coding, working, family, japanese learning and much more… it’s really hard to get some additional time. ๐Ÿ˜ฆ But I saw your Blog and it looks very nice.Keep it forward! ๐Ÿ™‚ Best regards!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s