Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Andrew Dahl
/
Project Euler
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a8894e8c
authored
Oct 07, 2012
by
Andrew Dahl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Solved Problem 26
parent
34a31c23
Hide whitespace changes
Inline
Sidebyside
Showing
2 changed files
with
65 additions
and
0 deletions
26/README
26/main.cpp
26/README
0 → 100644
View file @
a8894e8c
Question:
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1digit recurring cycle. It can be seen that 1/7 has a 6digit recurring cycle.
Find the value of d 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
Answer: 983
26/main.cpp
0 → 100644
View file @
a8894e8c
#include <iostream>
#include <vector>
using
namespace
std
;
const
int
MAX_INTEGER
=
1000
;
// Think of this as using simple long division.
// After we figure out the remainder, we add a zero to it and try again
// If we ever return to that remainder, it's looping!
int
find_recurring_pattern
(
int
numerator
,
int
denominator
)
{
vector
<
int
>
stack
;
int
i
=
0
;
while
(
true
)
{
numerator
=
numerator
*
10
;
numerator
=
numerator
%
denominator
;
for
(
int
it
=
0
;
it
<
stack
.
size
();
it
++
)
{
if
(
stack
[
it
]
==
numerator
)
{
return
(
i

it
);
}
}
stack
.
push_back
(
numerator
);
i
++
;
}
return
0
;
}
int
main
()
{
int
largest
=
0
;
int
denominator
=
2
;
int
temp
=
0
;
for
(
int
i
=
denominator
;
i
<=
MAX_INTEGER
;
i
++
)
{
temp
=
find_recurring_pattern
(
1
,
i
);
if
(
temp
>
largest
)
{
largest
=
temp
;
denominator
=
i
;
}
}
cout
<<
denominator
<<
endl
;
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment