Find the minimum positive integer divisible by both 2 and N - c++

I am still learning C++ and having an ECPC Competition tomorrow.
You are given a positive integer N. Find the minimum positive integer divisible by both 2 and N.
What should I do in the for loop? (still not completed)
#include <iostream>
using namespace std;
int main()
{
int n;
cin>> n;
for(int i; ???;i++)
return 0;
}

You don't need any loop.
There are two cases. Either N is not divisible by 2, then all integers divisible by N and 2 are of the form
x = N * 2 * y
The smallest of those x has y==1.
The second case is when N is divisible by 2, then all integers divisible by N and 2 are of the form
x = N * y
The smallest of those x has y==1.
TL;DR: Do the maths first!

The result is N if N is divisible by 2, otherwise it's N*2.
int result = (N%2 == 0) ? N : N*2;

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i,N;
scanf("%d",&N);
for(i=1;i<100;i++)
{
if(i%2==0&&i%N==0)
{
if(i<12)
{
printf("%d",i);
}
}
}
return 0;
}

you should try using
int i=0;
while(true)
{
i++;
if((i%2==0) && (i%n==0))
{
cout<<i;
break;
}
}

Related

Given a number n, print the first number of the series greater than or equal to n

I am trying to find an efficient way to, given a number n, print the first number of the following series greater or equal to n:
0, 0+1, 0+1+2, 0+1+2+3, 0+1+2+3+4, ... = 0, 1, 3, 6, 10, ...
For example, 2 would return 3, and 7 (or 8, or 9) would return 10.
There's surely a smart way to solve it... Could you help me? Thanks in advance!
The following code returns the correct solutions, but it is not efficient enough for this problem:
#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n){
int m = 0;
for(int i = 1;m < n; ++i){
m = m + i;
}
cout << m << endl;
}
}
You are describing triangular numbers!
y = (x*(x + 1)) / 2;
To get the next number in the series given y, you could solve for x
x = ceil((sqrt(1+8y)-1)/2);
Then throw x back into the 1st formula to get your answer!
This is a good approach for large numbers but may not be ideal for smaller numbers
Basically you want to write program for Arithmetic progression (AP) i.e. sum of first n numbers
int main(){
int n;
while(cin >> n){
int m = (n * (n + 1)) / 2;
cout << m << endl;
}
}

How do i determine the first digit of a number without knowing the number of digits?c++

For example, 42556. How do I determine first digit if I don't know the number of digits in the number? I could't find an algorithm that suits me anywere! (I mean to determine the 4 in 42556)
Assuming a is the input number.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long a = 42556;
long num;
num=floor(log10(a))+1;
//cout<<num<<" "<<"\n"; //prints the number of digits in the number
cout<<a/(int)pow(10,num-1)<<"\n"; //prints the first digit
cout<<a%10<<"\n"; //prints the last digit
return 0;
}
Live demo here.
You could keep on dividing it by 10 until you've reached the last digit:
int lastDigit(int n) {
n = abs(n); // Handle negative numbers
int ret = n;
while (n > 0) {
ret = n % 10;
n /= 10;
}
return ret;
}
Iteratively divide by 10 until the result is less than 10.
num = 42556
while num > 9
num = num / 10
All answers assumed you have an integer number. But generally, you can first get the integer form using the function floor from <cmath>, i.e.
#include <cmath>
int getlastdigit(double number)
{
long long n = (long long)floor(number);
while(n > 9)
n /= 10;
return n;
}
Try this:
int firstdigit(int n) {
int x = n;
while(n != 0) {
x = n%10;
n = n/10;
}
return x;
}

Memory + dynamic programming or recursion

Problem is that i have 64 megabytes on solution,so i can use only 16,777,216 int numbers.
But for answer i must use 33,333,333 numbers,so some answers will not be considered.
Actually, problem is this.
By the way, i had my own code instead:
#include <iostream>
using namespace std;
int sq(int x) {
return (long long)(x*x) %(1000000);
}
int func(int x) {
if (x==0)
return 3;
else {
return ( sq(func(x-1))+2)%(1000000);
}
}
int main()
{
/*const int num=16 777 216;
int* arr=new int [num];
arr[0]=3;
for (int i=1;i<num;i++)
arr[i]=((arr[i-1]%(1000000))*(arr[i-1])%(1000000)+2)%(1000000);*/
int t,rez;
int n;
cin>>t;
for (int p=0;p<t;p++) {
cin>>n;
if (n%3!=0) {
rez=0;
} else {
// rez=arr[n/3-1];
rez=func(n/3-1);
}
cout<<rez<<endl;
}
return 0;
}
In comments there is second solution.
I can do it with recursion, but i have limit in 1 second.
So what code would be OK?
You do not need anywhere near that many entries (10^9 / 3). Note that you need only the values mod 10^6. You have a recurrence relationship among these:
a[n] = a[n-1]^2 + 2
Each value depends only on the previous one, so there will be a simple sequence of numbers. The relation will have a period of no more than 10^6. Since they're all odd, the maximum length is cut in half.
As it turns out, the sequence repeats after 5003 terms, with a period of 5000: 3, 11, 123 do not appear later in the sequence.
So, forget that huge array. Compute the 5003 terms you need. Now for any input number N, you have 3 cases:
(1) N is not divisible by 3: return 0
else N is divisible by 3; call the quotient M
(2) M <= 3: return arr[M]
(3) else, get the needed subscript as m = ((M-3) mod 5000) + 3;
return arr[m]
You can now handle arbitrarily large input.

Greatest Common Divisor using Euclidian Algorithm?

So I'm having a problem with my code here.
I am coding a Greatest Common Divisor using the Euclidian Algorithm and I can't seem to utilize the loop in order to keep the division to keep repeating until I get the greatest common divisor. So for now, I am able to get the remainder but do not know how to go on from there basically.
Any help will be greatly appreciated!
Here is what I have so far
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int a;//Holds the first number
int b;//Holds the second number
int temp;//Assign to greatest number
int hold;//Assign to smaller number
float euclid;//soon to be function?
int leftover;
float gcd;
int main ()
{
cout<<"Welcome to Brian Garnadi's Version of GCD!\n"<<endl;
cout<<"Enter the first integer to be calculated: ";
cin>> a;
cout<<"Now enter the second integer: ";
cin>>b;
if (a>b)//Determines bigger number
{temp=a;
hold=b;
}
if (a<b)//Determines smaller number
{
temp=b;
hold=a;
}
leftover= temp%hold;
cout<<"\nThe remainder of the two numbers divided is "<<leftover<<".\n"<<endl;
}
Actually there is no need to calculate bigger number the euclid's algorithm manages itself.
Here's the working code :
#include <iostream>
using namespace std;
int gcd(int m,int n)
{
if(n == 0)
return m;
return gcd(n, m % n);
}
int main()
{
int a,b,answer;
cout<<"Welcome to Brian Garnadi's Version of GCD!\n"<<endl;
cout<<"Enter the first integer to be calculated: ";
cin>> a;
cout<<"Now enter the second integer: ";
cin>>b;
answer = gcd(a,b);
cout << "The GCD of the two numbers is : " << answer << endl;
return 0;
}
Do not forget to handle negative numbers in algorithm.
gcd(m, n) = gcd(n, m%n) when n != 0
= m when n = 0
function:
int gcd(int m, int n) {
if(m == 0 && n == 0)
return -1;
if(m < 0) m = -m;
if(n < 0) n = -n;
int r;
while(n) {
r = m % n;
m = n;
n = r;
}
return m;
}

Code for Armstrong numbers not working

Alright please go easy. Just learning C++ and first also question here. I've written a program to list all Armstrong numbers below 1000. While I have read the Wikipedia article on narcissistic numbers, I'm only looking for 3-digit ones. Which means I only care for the sum of the cubes of the digits.
It works by executing a for loop for 1 to 1000, checking whether the indexing variable is armstrong or not using a user defined function and printing it if it is. The user defined function works simply by using a while loop to isolate digits and matching the sum of the cubes to the original number. If it is true, then returns 1 otherwise return 0.
The problem is, I'm getting abolutely no numbers in the output. Only the cout statement in void main() appears and the rest is blank. Tried to debug as much as I could. Complier is Turbo C++. Code-
#include<iostream.h>
#include<conio.h>
int chk_as(int);//check_armstrong
void main()
{
clrscr();
cout<<"All Armstrong numbers below 1000 are:\n";
for(int i=1;i<=1000;i++)
{
if (chk_as(i)==1)
cout<<i<<endl;
}
getch();
}
int chk_as (int n)
{
int dgt;
int sum=0,det=0;//determinant
while (n!=0)
{
dgt=n%10;
n=n/10;
sum+=(dgt*dgt*dgt);
}
if (sum==n)
{det=1;}
else
{det=0;}
return det;
}
The problem is that you are dynamically changing the value of n in your method, but you need its original value to check the result.
Add in a temporary variable, say, t.
int t = n;
while (t!=0)
{
dgt=t%10;
t=t/10;
sum+=(dgt*dgt*dgt);
}
if (sum==n)
// ... etc.
EDIT: Nevermind... this was wrong
while (n!=0)
{
dgt=n%10;
n=n/10;
sum+=(dgt*dgt*dgt);
}
This runs forever as n never reaches 0.
The problem is, that in the end of the loop
while (n!=0)
{
dgt=n%10;
n=n/10;
sum+=(dgt*dgt*dgt);
}
n is 0, so the condition if (sum==n) is never true.
Try something like :
int chk_as (int n)
{
int copy = n;
int dgt;
int sum=0,det=0;//determinant
while (copy!=0)
{
dgt=copy%10;
copy=copy/10;
sum+=(dgt*dgt*dgt);
}
if (sum==n)
{det=1;}
else
{det=0;}
return det;
}
I have given here the program for finding armstrong number of a three digits number.
The condition for armstrong number is,
Sum of the cubes of its digits must equal to the number itself.
For example, 407 is given as input.
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an armstrong number.
#include <stdio.h>
int main()
{
int i, a, b, c, d;
printf("List of Armstrong Numbers between (100 - 999):\n");
for(i = 100; i <= 999; i++)
{
a = i / 100;
b = (i - a * 100) / 10;
c = (i - a * 100 - b * 10);
d = a*a*a + b*b*b + c*c*c;
if(i == d)
{
printf("%d\n", i);
}
}
return 0;
}
List of Armstrong Numbers between (100 - 999):
153
370
371
407
Reference: http://www.softwareandfinance.com/Turbo_C/Find_Armstrong_Number.html