473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Project Euler #29 alternative solution

13
This is a question about a Project Euler problem. This is where you can find the description of the problem: https://projecteuler.net/problem=29

OK, first of all, let me clarify that I have solved this problem, I am just looking for an alternative solution that is based more on maths.
Second, in order to not spoil the problem for anyone that has not solved, please if you have not solved it, don't continue. :)

So, I solved this problem using Python and because it has support for big numbers and list comprehensions, I was able to come up with an one liner:

Expand|Select|Wrap|Line Numbers
  1. print(len(set([a ** b for a in range(2, 101) for b in range(2, 101)])))
Now, I am trying to solve it in C, by using more mathematical knowledge(C natively has no support for big numbers, or list comprehensions). I came across this thread: http://stackoverflow.com/questions/2...oject-euler-29 where the accepted answer gave me some ideas and I came up with this code:

Expand|Select|Wrap|Line Numbers
  1. int main(void) {
  2.  
  3.     int found[1000];    // an array where I hold the found values(0 or 1)
  4.     int e, b, i, count, x;
  5.  
  6.     count = 0;     // number of duplicates
  7.     x = 3;
  8.     for(i = 0; i < 1000; i++)
  9.          found[i] = 0;
  10.  
  11.  
  12.     for(e = 1; (int)pow(x, e) <= 100; e++) {
  13.         for(b = 2; b <= 10; b++) {
  14.             if(found[e * b])    // if the value has been found, then we have duplicate
  15.                 count++;
  16.             found[e * b] = 1;   // mark that we found the value
  17.         }
  18.     }
  19.  
  20.     printf("count: %d\n", count);
  21.  
  22.     return 0;
  23. }
With this code, I am doing what you can see on the bottom of the answer above, where he shows some diagrams on how the to find the duplicates for x = 3, based on what he has explained previously. I am trying to do the same thing. Now, if you run my code, it correctly outputs 13, based on the diagrams of the above answer, which is the number of the duplicates.

So, I tried to extend it to solve the actual project euler problem because if I am able to find the number of duplicates, then I will just subtract it from the number 99 * 99(which is the possible power combinations because 2 <= a <= 100 and 2 <= b <= 100) and that would be the answer. The result was that:

Expand|Select|Wrap|Line Numbers
  1. int main(void) {
  2.  
  3.     int found[1000];
  4.     int e, b, i, count, x;
  5.  
  6.     count = 0;
  7.     for(x = 2; x <= 100; x++) {
  8.         for(i = 0; i < 1000; i++)
  9.              found[i] = 0;
  10.  
  11.  
  12.         for(e = 1; (int)pow(x, e) <= 100; e++) {
  13.             for(b = 2; b <= 100; b++) {
  14.                 if(found[e * b])
  15.                     count++;
  16.                 found[e * b] = 1;
  17.             }
  18.         }
  19.     }
  20.  
  21.     printf("count: %d\n", count);
  22.  
  23.     return 0;
  24. }
If you pay attention, the changes are that I loop for all the xs from 2 to 100 and b is not going from 2 to 10 but from 2 to 100.
However, the program prints 814, which is incorrect. It should be 618. Any help is highly appreciated! I am probably counting some duplicates twice, but where? What is wrong with the code? Also, if you have any mathematical explanations that would help construct a new algorithm, this is highly appreciated too!

EDIT: Something that I forgot to mention is that if instead of putting:
for(x = 2; x <= 100; x++) I do:
for(x = 2; x <= 6; x++)
i.e. stop to 6, it prints the correct answer. And that is even more bizzarre.
Jan 23 '16 #1
0 1332

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Montrose... | last post by:
Open-Source Project Offers Alternative to Visual Studio Team System Fri Apr 8,12:35 PM ET The project, known as NTeam, will use several existing open-source tools and applications, such as the...
1
by: lukwagoroy | last post by:
Loaded 'ntdll.dll', no matching symbolic information found. I'm writing a small program. It compiled and run. But when trying to debug by select, I got the following error messages: Loaded...
3
by: Tee | last post by:
Hi Does anyone know how to run another project that inside the same solution ? The project that going to be run is a windows application project. If I don't want to run the EXE, do I have any...
2
by: Sidney | last post by:
Hi, I have two project in one solution, one is VB and the other is C#, I want to know...is it ok? Second, if it is ok, how can I call the C# function in VB project? Thank You........ Sidney
5
by: cmay | last post by:
I have a big solution filled with a bunch of projects. I add a project to the solution (not adding any references mind you). Now, if I close down everything and try to just open that single...
4
by: Anbu | last post by:
Hi All, I'm creating an ASP.NET 2.0 web solution using VS 2005. The solution (.sln) contains 4 projects, one depends on another. I need to compile the project files using automated process,...
0
by: lilane | last post by:
I have an mdiparent but now i tried to add a childform that it is a mdiparent too, but vb .net gives me an error because mdiparent can not be a mdichild so i need your help to find an alternative...
25
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not...
8
by: Mathismylove | last post by:
Hi. Im new to Java and attempted to increase my knowledge by working on Project Euler..I figured that If Im able to solve as many problems from the project as possible with Java I will get more...
4
by: liams | last post by:
So, a few days ago I decided to try Project Euler question #22. The question is: Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.