difficulty with prob.3 on project euler (in c++) [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
so, I've been trying to solve this problem for a few hours now.
shortly I arrived to a solution that logically - should work and is working, but only for numbers no bigger than 10^7. I guess I could just const the specific number they asked for (600851475143) but I would really love to know - why my code isn't working with big numbers?
this is my code for the solution :
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
//enter any number and its largest prime factor will be detected.
int main()
{
long largest(1),num(0);
bool primecheck;
cout<<"enter the desired number :"<<endl;
cin>>num;
if (num%2==0)
largest=2;
cout<<"the relevant factors are: ";
for (int i=3;i<=int((sqrt(num))/2);i+=2)
{
primecheck=true;
for(int j=2;j<i;j++)
{
if(i%j==0)
primecheck=false;
}
if(primecheck)
if(num%i==0)
{
largest=i;
cout<< largest<<"\t";
}
}
cout<<endl<< "the largest prime factor of the number you have entered is: " <<largest;
return 0;
}
thanks in advance! :)

Problem with your code-
Use long long int as data type instead of long int since C++ recognizes long int as having same range as int.
If you want to refer my concise solution in C/C++ -
*For C++ just change the header file to iostream.
#include <cstdio>
#define MAX 775147
#define NUM 600851475143
int main()
{
long long n = NUM;
int max = 3;
for(int i = 3; i <= MAX; i+=2)
{
if(n%i == 0)
max = i;
while(n%i == 0)
{
n /= i;
}
}
printf("%d\n", max);
return 0;
}

Related

in the c++ hacker rank preperation cause, the last index returns 0 when i reverse it. but when i try the code out in visual studio, it works perfectly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
HERE IS THE QUESTION I FACED ON HACKERRANK.
the hackerrank question
HERE IS THE CODE I PRINTED
#include <iostream>
using namespace std;
int main() {
int n;
int array[n];
int c,x;
cin>>n; //inputting the array size
n=n+1;
int m=n;
if(n>=1 && n<=1000 && m>=1 && m<=1000 )
{
for(c=1;c<=n;c++)//inutting the numbers
{ if(c<=10000)
{
cin>>array[c];
}
}
for(x=m-1;x>=1;x--)//outputing the numbers reversed
{
cout<<array[x]<<" ";
}
}
return 0;
}
WHEN I GIVE INPUT THE ARRAY SIZE AS 4 AND THE DATA AS 1 4 3 2,
ITS REVERSED AS 2,3,4,0
BUT IT PERFECTLY GETS REVERSED WHEN COMPILED WITH VISUAL STUDIO CODE. WHYS THAT?? AND ALSO WHEN I SUBMIT CODE SEVERAL TESTS PASS, BUT SOME GET A SEGMENTATION ERROR
As mentioned in the comments, array should be initialized with a proper capacity. You should first read the size and then create the array.
#include <iostream>
using namespace std;
int main() {
int n;
// First read the array size and then create the array.
cin>>n; //inputting the array size
int array[n];
//inputting the numbers
// Remember indices go from 0 to N-1
for(int c=0;c<n;c++)
{
cin>>array[c];
}
//outputing the numbers reversed
for(int x=n-1;x>=0;x--)
{
cout<<array[x]<<" ";
}
return 0;
}

I'm getting Runtime Error in a problem from Code Forces [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is the problem: https://codeforces.com/contest/1256/problem/D .I'm getting runtime error("out of bound") in test case 15 here is my submission: https://codeforces.com/contest/1256/submission/84865113
Where am I doing wrong?
my approach:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
string num;
cin>>num;
int start = 0;
for(int i=0;i<n;i++){
if(num[i]=='0' && k>0){
if((k-abs(i-start))>=0){
swap(num[start],num[i]);
k = k - abs(i-start);
start++;
}
else{
swap(num[i],num[i-k]);
k = 0;
}
}
}
cout<<num<<"\n";
}
return 0;
}
the range of k is out of int. You should use long long.
9428683473 is out of 2147483647(max value for type int), so you should not use int. And the var i and start should change too.

Why is my C++ program not working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to build a program that reads numbers from "bac.txt" and returns the 2 digit number/numbers (10,11,12,...,99) that appear most frequently. For example if the file "bac.txt" contains 393 17775787 72194942 12121774 it will return 77 and 21. I have managed to build a working function counter(int n) that can count how many times n is found in the file and returns i which is the number of times n has been found. Now I can't seem to find a way to print to the screen the number/numbers that are found most often. I tried to use some kind of for loop but it doesn't work.
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int counter(int n){
int i=0,j=0;
char x1=n/10+'0';
char x2=n%10+'0';
char a;
char b=NULL;
fstream fisier("bac.txt", fstream::in);
while (fisier >> noskipws >> a) {
if(b==x1&&a==x2)
i++;
b=a;
}
return i;
}
int main()
{
int v[90];
int v1[90];
int i,maxim=0,nr;
for(i=10;i<100;i++)
{
v[i]=counter(i);
if(v[i]>maxim)maxim=v[i];
}
for(i=10;i<100;i++)
if(v[i]==maxim)
cout<<i;
}
I have corrected the code.It works now.
You may check the changes.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int counter(int n)
{
int i = 0, j = 0;
char x1 = n / 10 + '0';
char x2 = n % 10 + '0';
char a;
char b;
fstream fisier("bac.txt", fstream::in);
fisier >> b;
while (fisier >> a) {
if (b == x1 && a == x2)
i++;
b = a;
}
return i;
}
int main()
{
int v[101];
int i, maxim = 0, nr;
for (i = 10; i < 100; i++) {
v[i] = counter(i);
if (v[i] > maxim)
maxim = v[i];
}
for (i = 10; i < 100; i++)
if (v[i] == maxim)
cout << i;
}
i can't say you why your program's not working, but i can say you how i'd solve it. you have to store in an int vector the 2 digit number that you read from file (ex. if file contains 1234 the int vector will contains 12 23 34) than you have to find the 2 number that appear most times, so, you have read the first vector element and store it in a variable, than you count how many times it appear in the vector and you save the number you were searching in max_a and the times it appear in max_times_a (remember that when you find in the vector the number you're searching you have to put a -1 on it) than you search in the vector another number and count how many times it appear (putting -1 on that number) than you store that number in max_b and the times it appears in max_times_b. than what you have to do is to slide the vector and if you reach the end becouse you only read -1 you have max_a and max_b as result, but if you find a number you have to count how many times it appear and checking if it appear most times of max_a or max_b, and in case it appear most times you have to swap the values. i hope to have been useful to you.
Tommaso

Why is my solution to the 3n+1 puzzle being rejected? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to solve algorithmic problems on the UVa online judge but I am stuck on the 3n+1 problem. I see the right output every time, but the judge says it is a wrong answer. Why?
Also, how can I optimize this code so that it does not take a long time for 1000000?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main(){
int a;
int b;
int cyclelength(int i);
while (scanf("%d %d\n",&a,&b)==2){
int max = 0;
if (b < a){
for (int i = b; i < a; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
else
{
for (int i = a; i < b; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
cout << a << " " << b << " " << max << endl;
}
}
int cyclelength(int i){
if(i==1)
return 1;
if(!(i%2))
return cyclelength(i/2)+1;
else
return cyclelength(3*i+1)+1;
}
Your submission is incorrect because you aren't iterating over the range from a to b, inclusive.
The problem statement reads:
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j.
Your code loops over
for (int i = b; i < a; i++)
and
for (int i = a; i < b; i++)
thus omitting a in the former case and b in the latter.
You should change the loops to
for (int i = b; i <= a; i++)
and
for (int i = a; i <= b; i++)
You can speed up your code somewhat by memoizing the results of cyclelength. This lets you look up previously computed values instead of spending time on computing them again.
Here is a memoized version of cyclelength:
#include <map>
using namespace std;
map<long long, int> memo;
int cyclelength(long long i) {
if (i == 1) {
return 1;
}
if (memo.count(i) == 1) {
return memo[i];
}
if (i%2 == 0) {
return memo[i] = 1 + cyclelength(i/2);
} else {
return memo[i] = 1 + cyclelength(3*i + 1);
}
}
An unordered_map would be faster, but the UVA judge returned a compilation error for #include <unordered_map>.
Note that my version of cyclelength takes a long long argument. That lets me calculate cyclelength(999999), which eventually causes an integer overflow due to repeated 3n+1 operations if you limit yourself to 32-bit integer values. The problem statement promises that "no operation overflows a 32-bit integer" and you can get your submission accepted if you only use int values, but you have to use a larger integer type if you want to compute cyclelength for values in the range [1, 1000000).

C++ program calculates n times n for the numbers 1 to 9 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to write a program that calculates n times n for the numbers between 1 and 9 using a function.
The output should be like 1, 4, 27, 256...
I can feel I'm very close to finishing it but I just can't figure out what the problem is, here is the code I wrote:
#include <iostream>
using namespace std;
int result, number, n;
void function1()
{
result = number;
for (int x = 1; x < number; x++)
{
result = number*result;
}
}
int main()
{
for (n = 1; n < 10; n++)
{
function1();
cout << result << endl;
system("pause");
return 0;
}
}
Try this :-
#include <iostream>
#include <math>
using namespace std;
int result, number, n;
void function1(int number)
{
int result;
result = pow(number,number);
cout<<result;
}
int main()
{
for (n = 1; n < 10; n++)
{
function1(n);
system("pause");
}
return 0;
}