algorithm optimization - smallest number having n divisiors [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This problem is a programming version of Problem 12 from projecteuler.net..
The sequence of triangle numbers is generated by adding the natural numbers. So the 7'th triangle number would be 1+2+3+4+5+6+7=28. The first ten terms would be:
1,3,6,10,15,21,28,36,45,55,...
triangle number 28 would be having following factors
28 = 1,2,4,7,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over N divisors?
(1 <= N <= 1000)
I have written code which is working for N=750.But it is taking long time for N=1000.
#include <iostream>
#include <stdio.h>
#include <vector>
#define LL long long
using namespace std;
int main()
{
int test;
scanf("%d",&test);
int v[10001]={0};
int tnum=1,num=1;
while(1)
{
int c=0;
for(int i=1;i*i<=tnum;++i)
{
if(tnum%i==0)
{
++c;
if(i!=(tnum/i))
{
++c;
}
}
}
if(v[c]==0)
v[c]=tnum;
// cout << "c = "<<c<<" tnum = " << tnum << endl;
if(c>1000)
{
break;
}
++num;
tnum += num;
}
while(test--)
{
int n;
scanf("%d",&n);
++n;
while(v[n]==0)
{
++n;
}
printf("%d\n",v[n]);
}
return 0;
}
Any help or suggestions would be appreciated. Thanks.
Edit 1 = Answer for this question - As we have to find the next triangle number having over n divisiors,we can brute force all triangle number and find their number of divisiors. Then we will see that a pattern for larger value of n(n>240)

If I told you how many divisors the number 938,839 has, and how many divisors the number 938,840 has, how would you find out the number of divisors of 938,839 * 938,840, using just the information that I gave you?
And how can you use that idea to make your algorithm run about 100 times faster?

Related

Codeforces Problem 110A - Trouble with a certain input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Problem:
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Unfortunately, not all numbers are lucky. Petya calls a number nearly lucky if the number of lucky digits in it is a lucky number. He wonders whether number n is a nearly lucky number.
Input
The only line contains an integer n (1 ≤ n ≤ 1018).
Output
Print on the single line "YES" if n is a nearly lucky number. Otherwise, print "NO" (without the quotes).
I submitted a solution on this problem and fail at the input of 4744000695826. This should give an output of YES, since it has 4 lucky numbers, and 4 is a lucky number itself. However I get an output of NO. This was the first input test which contained 4s and 7s and was bigger than 32 bit int limit 232-1, so I guess that has something to do with it, but I honestly don't know.
Here is my code:
#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
// counting digits in an int
int count_digit(int number) {
return int(log10(number) + 1);
}
// counting certain digits in an int
int counter(int b, int n) {
int count = 0;
while(b > 0) {
if(b % 10 == n) {
count++;
}
b /= 10;
}
return count;
}
int main()
{
int k;
cin >> k;
int r = counter(k,4) + counter(k,7);
if (count_digit(r) - (counter(r,4) + counter(r,7)) == 0) {
cout << "YES";
}
else {
cout << "NO";
}
return 0;
}
If you want to process numbers greater than your ints, you need to use something else.
For example, you could:
std::string number;
std::cin >> number;
Then you can check the number with
char value = '5';
for(const char& c: number)
{
if (c == value);
}
That way you're not even limited to 64 bits.
Even more modern/idiomatic would be to use std::count:
https://en.cppreference.com/w/cpp/algorithm/count

i want to find factorial of 'n' number of numbers in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to find factorial of n number of numbers and it seems the code is right. But why does the program output a wrong answer?
Sample input
4
1
2
3
4
Samplw Output
1
2
6
24
See the output is correct but idk why the website says wrong answer?
#include <iostream>
using namespace std;
int main() {
int t, n, f;
cin >> t;
while (t > 0) {
f = 1;
cin >> n;
while (n > 0) {
f = f * n;
n--;
}
cout << f << "\n";
t--;
}
return 0;
}
use long long int instead of int. if you need something accurate for large numbers use gmp instead.
Taking into account that factorial is positive you can also can use unsigned long long to enlarge 1 more bit of data (this does not help though in the case of factorial function, conforming to the series A000142).
This can accurately keep the result for maximum ULLONG_MAX ( 18446744073709551615=2^64-1), the biggest factorial in the limit of long long type is, conforming to the The On-Line Encyclopedia of Integer Sequences, 2432902008176640000, which is factorial(20).

Get number of digits in an int without divided by 10 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is there a way to get the number of digits without the division by 10?
For example i have this:
int main()
{
int dividend = 100;
int remainder=0;
int temp = 0;
while(dividend>=10)
{
dividend = dividend-10;
temp+=1;
}
printf("Quotient: %d\n",temp);
printf("Reminder: %d\n",dividend);
}
And now I will add to calculate the number of digits of the variable dividend.
You have to know the maximum range of integer to make this function usefull.
no function call, no division ...
int nbDigitInteger(int number)
{
if (-10 < number && number < 10) return (1);
if (-100 < number && number < 100) return (2);
if (-1000 < number && number < 1000) return (3);
if (-10000 < number && number < 10000) return (4);
if (-100000 < number && number < 100000) return (5);
...
}
Sometime, the simplier is the best.
If you are allowed to use logarithms then
int i = 123456;
int digitsCount = ceil(log10(abs(i)+1.0));
cout << digitsCount;
6
Your question is too broad, and the code is also unrelated.
Since you attempted to post the code, I'll provide the guidelines for the problem you asked for. Write the code yourself.
Take the absolute integer value. (abs())
Print it to a (large enough) buffer. (sprintf()/ snprintf()).
Use strlen() to get the length of the buffer (as string).
An alternative to the very elegant solution o #Yola is this.
intPow10 is returning 10 to the power exponent. I did not use pow from math.h, since it is numerically expensive and as #Tom's pointed out it can lead to invalid results.
#include <stdio.h>
#include<math.h>
int intPow10(int exponent){
int retval=1;
while (exponent){
retval *=10;
exponent --;
}
return retval;
}
int numDigits(const int i) {
int digits = 1;
while (intPow10(digits) <= fabs(i)) {
digits++;
}
printf("%i has %i digits.\n", i,digits);
return digits;
}
int main() {
numDigits(1);
numDigits(-1);
numDigits(10);
numDigits(13);
numDigits(-112312);
}
Is this code golf or what?
int b = 1000;
char a[10] = itoa(b);
printf("%d\n", strlen(a)); // 4
This simply turns b into a string, which is a. Then, prints the length. What would we do without atoi() and itoa()? Our own functions!

c++ how to store a variable and string that is 2^1 000 000 large? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to solve an exercise. It says that i need to output 3 last digits of a number that is 2 raised to the power of n (2^n).
But input is n=1000000.
The code works with lower values, but when the input is 1 000 000 the number gets too large.
My code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
unsigned long long n;
cin >> n;
unsigned long long sk = pow(2, n);
if (sk < 1000) cout << sk;
else {
string ats = to_string(sk); // converting the number to string
// so I could output 3 last digits
// probably not the best solution
// for this exercise
n = ats.length();
for (unsigned long long i = n - 3; i < n; i++) {
cout << ats[i];
}
}
return 0;
}
Thank you for your help.
Try something like:
Initialize result to 1
Within a loop from 1 to n:
result *= 2
result %= 1000
This because the result of the last 3 digits does not depend upon the greater digits
You can calculate your number modulo 1000. Just saving the three least significant digits and trashing the more significant ones does not alter the result.
Just do:
int result = 1;
for(int i = 0; i < n; i++){
result = (result * 2) % 1000;
}

How to calculate how many times a number goes into another number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is the program with the initial 'number' stated in the question taken as 'n' and the 'other number' taken as 10.
void divideme()
static int count=0; //initalised a variable which I'll be returning the value of.
int n;
cin>>n;//taken input of variable which I want to divide by another number (say 10 in this case)
int &rem=n;//created a reference variable which stores the value of n.
while (rem>=10) {
rem=rem%10; //this is to be corrected as rem = rem - 10;
count++;
}
return count;
Your code is overkill. Just do the division one time. The result is the number of times 10 goes into the number. No loop is needed at all. The % operator gives you the modulus (remainder) of a division, which is not what you need in this situation.
int divideme()
{
int n;
cin>>n; //get input which I want to divide by another number (say 10 in this case)
return (n / 10);//return how many times it divides by 10
}
For example:
9 / 10 = 0
9 % 10 = 9
10 goes into 9 0 times, with a remainder of 9.
12345 / 10 = 1234
12345 % 10 = 5
10 goes into 12345 1234 times, with a remainder of 5.
The % operator give you the modulus, which is the remainder after division.
If you just want to count the number of times that 10 goes into a number rem, then replace
rem=rem%10;
with
rem = rem - 10;
in your loop.
(Also, you don't need if (rem>=10) in your code. The while loop takes care of this.)
#include <cmath>
#include <iostream>
int times_divided_by_10(int x)
{
return int(std::log10(double(x)));
}
int main()
{
std::cout << times_divided_by_10(101) << std::endl;
}
expected output:
2
another way:
#include <iostream>
int times_divided_by_10(int x)
{
int count = 0;
while (x >= 10) {
++count;
x /= 10;
}
return count;
}
int main()
{
std::cout << times_divided_by_10(101) << std::endl;
}