Adam Lesperance > Project-Euler > Project::Euler::Problem::P001

Download:
Project-Euler-0.20.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.20   Source  

NAME ^

Project::Euler::Problem::P001 - Solutions for problem 001 - Sum filtered range

VERSION ^

version 0.20

HOMEPAGE ^

http://projecteuler.net/index.php?section=problems&id=1

SYNOPSIS ^

    use Project::Euler::Problem::P001;
    my $p1 = Project::Euler::Problem::P001->new;

    my $default_answer = $p1->solve;

    #  Use the default filter list of '3, 5'
    $p1->solve(11);  #  3 + 5 + 6 + 9 + 10  ==  33

    #  Didn't override the default answer so status is false!
    $p1->status;  # 0


    #  Change the filter list
    $p1->multi_nums( [4] );
    $p1->solve(25, 84);  #  4, 8, 12, 16, 20, 24  ==  84

    #  Overrode the default answer with the right one so the status is true
    $p1->status;  # 1

DESCRIPTION ^

This module is used to solve problem #001

This problem simply needs to find the sum of all the numbers within a range which are multiples of a set of integers. The range always starts at 1 and continues up to the provided input (1000 by default). The numbers are filtered using Project::Euler::Lib::Utils.

ATTRIBUTES ^

multi_nums

An array of positive integers that are used to filter out the number to sum.

This array is always kept sorted in order to optimize the solve function

Isa

PosIntArry

Default

[3, 5]

SETUP ^

Problem Number

    001

Problem Name

    Sum filtered list

Problem Date

    2001-10-05

Problem Desc

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.

Default Input

The maximum value

1,000

Default Answer

    233,168

Has Input?

    Yes

Help Message

You can change multi_nums to alter the way the program will function. If you are providing custom_input, don't forget to specify the wanted_answer if you know it!

INTERNAL FUNCTIONS ^

Validate Input

The restrictions on custom_input

    A positve integer

Solving the problem

Loop from the first multi_num up to the max_number and filter all numbers that are not multiples of one/all of the multi_nums. Then use the List::More util 'sum' to return the sum of the filtered numbers. If nothing was found return 0 rather than undef.

ACKNOWLEDGEMENTS ^

AUTHOR ^

Adam Lesperance <lespea@gmail.com>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2010 by Adam Lesperance.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: