So, here is my code,
int
is_prime(int m) {
int i, prime;
if (m == 2)
prime = 1;
if (!(m % 2))
prime = 0;
if (m < 2)
prime = 0;
for (i = 2; i <= sqrt(m); i++) {
if (m % i == 0) {
prime = 0;
break;
}
}
return prime;
}
int
main() {
int m, n, t_m, t_n;
while (scanf("%d %d", &n, &m) != EOF) {
t_n=n;
t_m=m;
while(1) {
int d = gcd(t_n, t_m);
t_n = t_n - 1;
if (d > 1) {
t_m = t_m / d;
if (t_n < t_m && is_prime(t_m)) {
printf("%d does not divide %d!\n", m, n);
break;
}
}
}
}
return 0;
}
It should break out from that if, right? I've tried putting it in a function and using the return statement, but that doesn't work, either.
Related
I am writing a program in order to count all achilles numbers without the math.h library. In this programm first I calculate powerful numbers after that I calculate GCD of powerful number. If GCD is 1 then the current number is achilles.
My problem is that my program works with scanf() but not with for loop counter! What i am doing wrong?
Thank you very much!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
for (a = 1; a <= MAX; a++) //for loop for counter
{
n=a;
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}
// printf("GCD(%d,%d)= %d", max, min, gcd);
printf("%d\n", achilles);
}
}
The programm before editing with the for loop is the following!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
scanf("%d", &n);
printf("%d = ",n);
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
/*if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}*/
printf("GCD(%d,%d)= %d", max, min, gcd);
//printf("%d\n", achilles);
}
I have make this program that calculates number factorization such as 60 = 2^2 * 5 * 3.
How can i modify my code in order to print POWERFUL NUMBERS such as 9000 = 2^3 * 3^2 * 5^3 without using math.h library and without using arrays?
Thank you very much!!
#include<stdio.h>
#define MAX 1000
int main(){
int num;
int counter;
int number;
char factorizationOutput;
int isAchiles = 0;
int factor=2;
for(counter=2;counter<=MAX;counter++){
isAchiles = 1;
number=counter;
int factor=2;
while(factor<number){
int power=0;
if(number%factor==0){
while(number%factor==0){
number=number/factor;
power++;
}
if(power == 1){
isAchiles = 0;
}
printf("%d^%d",factor,power);
if(number!=1)
printf(" X ");
}
factor++;
}
if(number!=1)
printf("%d^1.\n",factor);
if(isAchiles == 1){
printf("factorazation of number %d is: ",counter);
}
}
}
#include<stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
printf("%d = ", n);
for(int i = 1; i <= n; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
int l = 0;
if(count == 2)
{
while(n % i == 0)
{
l++;
n = n/i;
}
printf("%d^%d*", i, l);
}
}
}
int a,b,n;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d",&n);
for(a=1;a<=100;a++)
for(b=1;b<=100;b++)
if(a<b && a*a + b*b == n*n)
{
printf("(%d, %d, %d)\n",a,b,n);
}
/*else
{
printf("impossible \n");
}
*/
return 0;
if I delete 'else' the program runs correctly. But I want to make another function which can check the number has pythagorean numbers or not by using 'else' paragraph. But when I put 'else' paragraph in that code, the result is dizzy.... plz help me!!
Put braces around the nested code blocks.
int a, b, n;
int impossible = 1;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d", &n);
for (a = 1; a <= 100; a++) {
for (b = 1; b <= 100; b++) {
if (a < b && a * a + b * b == n * n) {
printf("(%d, %d, %d)\n", a, b, n);
impossible = 0;
}
}
}
if (impossible == 1) printf("impossible \n");
return 0;
Here is a possible answer
#include <stdio.h>
int power(int base, int power);
int main(){
int N;
printf("INput the Num: ");
scanf("%d", &N);
int a, b, c;
for(a = 0; a < N ; a++) {
for(b = 0; b< N; b++) {
if ((a < b) && (b < N - a - b)) {
if (power(a, 2) + power(b, 2) == power(N - a - b, 2)) {
printf("%d^2 + %d^2 = %d^2 \n", a, b, N-a-b);
}
}
}
}
}
int power(int base, int power) {
int result = 1;
for(int i = 0; i < power ; i++) {
result *= base;
}
return result;
}
The 2nd iteration of the for loop always skips the 1st iteration. In debugging mode, it starts with i=1, but as soon as it computes the if clause, i=2 and for the rest it goes nice. It's almost as if there's n hidden continue.
Original Code:
int n, k, i, j = 0;
int *key;
int x = 0;
int y = 0;
scanf ("%d%d", &n, &k);
key = malloc (n * sizeof (int));
for (i = 0; i < n; i++) {
scanf("%d", &key[i]);
}
for (i = 1; i <= n; i++) {
if (key[j % n] == i) {
x++;
} else {
y++;
j++;
}
}
The code this way works perfectly though:
while (1) {
for (i = 1; i <= n; i++) {
while (1) {
if (x == k) {
printf("%d",y);
return 0;
} else if (!(key[j % n] - i)) {
x++;
break;
} else {
y++;
j++;
}
}
}
}
This application will receive a number n. After receiving this number, the program has to show the n-th prime in the list of primes. For example, if the user enters 3, the program is supposed to display 5, because 5 is the third prime starting at 2. I know that something is wrong with my code but I don't know where the problem is and how I can fix it.
#include <stdio.h>
int main() {
int n, i, flag, prime;
int counter = 1;
scanf("%d", &n);
if (n == 1) prime = 2;
else
do{
prime = 3;
for (i = 2; i < prime; i++) {
flag = 1;
if (prime % i == 0) {
flag = 0;
}
}
if (flag == 1)
counter++;
prime++;
} while (counter != n);
if (counter == n)
printf("%d\n", prime);
return 0;
}
Fix sample of remains of your policy like this :
#include<stdio.h>
int main(void){
int n, i, flag, prime;
int counter = 1;
scanf("%d", &n);
if (n == 1)
prime = 2;
else {
prime = 1;
do{
prime += 2;
flag = 1;
for (i = 3; i < prime; i+=2){
if (prime % i == 0) {
flag = 0;
break;
}
}
if(flag == 1)
counter++;
} while (counter != n);
}
printf("%d\n", prime);
return 0;
}
You are resetting flag to 1 every time through the loop, so 'flag' will only tell you if prime is divisible by "prime-1", which of course it never is.